From 72470b5ddf7778d6a5adb4db3031c1ab4c414d1a Mon Sep 17 00:00:00 2001 From: shing19m Date: Sat, 10 Aug 2013 20:53:22 +0200 Subject: [PATCH] Adding the original version from the blog. --- pom.xml | 62 +++++++++++++++ .../controller/RestController.java | 76 +++++++++++++++++++ .../testprojekt/dto/PersonCreateDto.java | 66 ++++++++++++++++ .../lusiardi/testprojekt/dto/PersonDto.java | 75 ++++++++++++++++++ .../exceptions/ResourceNotFoundException.java | 12 +++ src/main/webapp/META-INF/context.xml | 2 + src/main/webapp/WEB-INF/spring-servlet.xml | 30 ++++++++ src/main/webapp/WEB-INF/web.xml | 28 +++++++ src/main/webapp/index.jsp | 12 +++ 9 files changed, 363 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/de/lusiardi/testprojekt/controller/RestController.java create mode 100644 src/main/java/de/lusiardi/testprojekt/dto/PersonCreateDto.java create mode 100644 src/main/java/de/lusiardi/testprojekt/dto/PersonDto.java create mode 100644 src/main/java/de/lusiardi/testprojekt/exceptions/ResourceNotFoundException.java create mode 100644 src/main/webapp/META-INF/context.xml create mode 100644 src/main/webapp/WEB-INF/spring-servlet.xml create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/index.jsp diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..06a27ad --- /dev/null +++ b/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + de.lusiardi + webbasics1 + 1.0-SNAPSHOT + war + + Java Webprogrammierung mit Spring und Maven (Teil 3) + + 3.0 + + + + UTF-8 + + + + + + org.springframework + spring-webmvc + 3.2.4.RELEASE + jar + + + javax.servlet + javax.servlet-api + 3.1.0 + provided + + + + org.codehaus.jackson + jackson-mapper-asl + 1.9.12 + jar + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.6 + 1.6 + + + + + + diff --git a/src/main/java/de/lusiardi/testprojekt/controller/RestController.java b/src/main/java/de/lusiardi/testprojekt/controller/RestController.java new file mode 100644 index 0000000..32871bf --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/controller/RestController.java @@ -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 persons = new ArrayList(); + + @RequestMapping(value = "persons", method = RequestMethod.GET) + @ResponseBody + public List 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(); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/lusiardi/testprojekt/dto/PersonCreateDto.java b/src/main/java/de/lusiardi/testprojekt/dto/PersonCreateDto.java new file mode 100644 index 0000000..e4d39e0 --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/dto/PersonCreateDto.java @@ -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; + } + +} diff --git a/src/main/java/de/lusiardi/testprojekt/dto/PersonDto.java b/src/main/java/de/lusiardi/testprojekt/dto/PersonDto.java new file mode 100644 index 0000000..02def98 --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/dto/PersonDto.java @@ -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; + } +} diff --git a/src/main/java/de/lusiardi/testprojekt/exceptions/ResourceNotFoundException.java b/src/main/java/de/lusiardi/testprojekt/exceptions/ResourceNotFoundException.java new file mode 100644 index 0000000..fd7b526 --- /dev/null +++ b/src/main/java/de/lusiardi/testprojekt/exceptions/ResourceNotFoundException.java @@ -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 { +} diff --git a/src/main/webapp/META-INF/context.xml b/src/main/webapp/META-INF/context.xml new file mode 100644 index 0000000..a69abe7 --- /dev/null +++ b/src/main/webapp/META-INF/context.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/spring-servlet.xml b/src/main/webapp/WEB-INF/spring-servlet.xml new file mode 100644 index 0000000..7c4709b --- /dev/null +++ b/src/main/webapp/WEB-INF/spring-servlet.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..f6daba4 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,28 @@ + + + + WebBasics3 + + + index.jsp + + + + spring + org.springframework.web.servlet.DispatcherServlet + 1 + + + + + spring + /rest/* + + + \ No newline at end of file diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp new file mode 100644 index 0000000..47a57e3 --- /dev/null +++ b/src/main/webapp/index.jsp @@ -0,0 +1,12 @@ +<%@page contentType="text/html" pageEncoding="UTF-8"%> + + + + + REST Service + + +

REST Service

+ + \ No newline at end of file