Adding the original version from the blog.

This commit is contained in:
shing19m 2013-08-10 20:53:22 +02:00
parent 65633a88c3
commit 72470b5ddf
9 changed files with 363 additions and 0 deletions

62
pom.xml Normal file
View File

@ -0,0 +1,62 @@
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.lusiardi</groupId>
<artifactId>webbasics1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Java Webprogrammierung mit Spring und Maven (Teil 3)</name>
<prerequisites>
<maven>3.0</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--
For jason mapping
-->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,76 @@
package de.lusiardi.testprojekt.controller;
import de.lusiardi.testprojekt.dto.PersonCreateDto;
import de.lusiardi.testprojekt.dto.PersonDto;
import de.lusiardi.testprojekt.exceptions.ResourceNotFoundException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class RestController {
private List<PersonDto> persons = new ArrayList<PersonDto>();
@RequestMapping(value = "persons", method = RequestMethod.GET)
@ResponseBody
public List<PersonDto> getPersons() {
return persons;
}
@RequestMapping(value = "persons", method = RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public PersonDto createPerson(@RequestBody PersonCreateDto in) {
PersonDto person = new PersonDto(in.getName(), in.getEmail());
persons.add(person);
return person;
}
@RequestMapping(value = "persons/{id}", method = RequestMethod.GET)
@ResponseBody
public PersonDto getPerson(@PathVariable(value = "id") int id) {
for (PersonDto person : persons) {
if (id == person.getId()) {
return person;
}
}
throw new ResourceNotFoundException();
}
@RequestMapping(value = "persons/{id}", method = RequestMethod.DELETE)
@ResponseBody
public boolean deletePerson(@PathVariable(value = "id") int id) {
PersonDto toDelete = null;
for (PersonDto person : persons) {
if (id == person.getId()) {
toDelete = person;
}
}
if (toDelete != null) {
return persons.remove(toDelete);
} else {
throw new ResourceNotFoundException();
}
}
@RequestMapping(value = "persons/{id}", method = RequestMethod.PUT)
@ResponseBody
public PersonDto updatePerson(@PathVariable(value = "id") int id, @RequestBody PersonCreateDto in) {
PersonDto toUpdate = null;
for (PersonDto person : persons) {
if (id == person.getId()) {
toUpdate = person;
}
}
if (toUpdate != null) {
toUpdate.setEmail(in.getEmail());
toUpdate.setName(in.getName());
return toUpdate;
} else {
throw new ResourceNotFoundException();
}
}
}

View File

@ -0,0 +1,66 @@
package de.lusiardi.testprojekt.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement // for jaxb
public class PersonCreateDto {
private String name;
private String email;
/*
* de.lusiardi.testprojekt.dto.TestDto does not have a no-arg default
* constructor. this problem is related to the following location:
* at de.lusiardi.testprojekt.dto.TestDto
*/
public PersonCreateDto() {
}
public PersonCreateDto(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String foo) {
this.name = foo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
int hash = 3;
hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 37 * hash + (this.email != null ? this.email.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PersonCreateDto other = (PersonCreateDto) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,75 @@
package de.lusiardi.testprojekt.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement // for jaxb
public class PersonDto {
private static int idCounter = 0;
private int id;
private String name;
private String email;
/*
* de.lusiardi.testprojekt.dto.TestDto does not have a no-arg default
* constructor. this problem is related to the following location:
* at de.lusiardi.testprojekt.dto.TestDto
*/
public PersonDto() {
}
public PersonDto(String name, String email) {
this.id = idCounter;
idCounter++;
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String foo) {
this.name = foo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.id;
hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 59 * hash + (this.email != null ? this.email.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PersonDto other = (PersonDto) obj;
if (this.id != other.id) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,12 @@
package de.lusiardi.testprojekt.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* This Exception will be mapped to a 404 result. Reason will be written on the
* exeption page.
*/
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Some static reason")
public class ResourceNotFoundException extends RuntimeException {
}

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/WebBasics3"/>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
beans für das mapping von json und xml
-->
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<bean id="jaxbMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
<ref bean="jaxbMessageConverter"/>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<context:component-scan base-package="de.lusiardi.testprojekt" />
<mvc:annotation-driven />
</beans>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>WebBasics3</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--
all restfull urls go here
-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

12
src/main/webapp/index.jsp Normal file
View File

@ -0,0 +1,12 @@
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>REST Service</title>
</head>
<body>
<h1>REST Service</h1>
</body>
</html>