Browse Source

Adding the original version from the blog.

shing19m 8 years ago
parent
commit
72470b5ddf

+ 62
- 0
pom.xml View File

@@ -0,0 +1,62 @@
1
+<project 
2
+    xmlns="http://maven.apache.org/POM/4.0.0" 
3
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
5
+            http://maven.apache.org/xsd/maven-4.0.0.xsd">
6
+
7
+    <modelVersion>4.0.0</modelVersion>
8
+
9
+    <groupId>de.lusiardi</groupId>
10
+    <artifactId>webbasics1</artifactId>
11
+    <version>1.0-SNAPSHOT</version>
12
+    <packaging>war</packaging>
13
+
14
+    <name>Java Webprogrammierung mit Spring und Maven (Teil 3)</name>
15
+    <prerequisites>
16
+        <maven>3.0</maven>
17
+    </prerequisites>
18
+    <properties>
19
+        <project.build.sourceEncoding>
20
+            UTF-8
21
+        </project.build.sourceEncoding>
22
+    </properties>
23
+
24
+    <dependencies>
25
+        <dependency>
26
+            <groupId>org.springframework</groupId>
27
+            <artifactId>spring-webmvc</artifactId>
28
+            <version>3.2.4.RELEASE</version>
29
+            <type>jar</type>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>javax.servlet</groupId>
33
+            <artifactId>javax.servlet-api</artifactId>
34
+            <version>3.1.0</version>
35
+            <scope>provided</scope>
36
+        </dependency>
37
+        <!--
38
+            For jason mapping
39
+        -->
40
+        <dependency>
41
+            <groupId>org.codehaus.jackson</groupId>
42
+            <artifactId>jackson-mapper-asl</artifactId>
43
+            <version>1.9.12</version>
44
+            <type>jar</type>
45
+        </dependency>
46
+    </dependencies>
47
+
48
+    <build>
49
+        <plugins>
50
+            <plugin>
51
+                <groupId>org.apache.maven.plugins</groupId>
52
+                <artifactId>maven-compiler-plugin</artifactId>
53
+                <version>3.1</version>
54
+                <configuration>
55
+                    <source>1.6</source>
56
+                    <target>1.6</target>
57
+                </configuration>
58
+            </plugin>
59
+        </plugins>
60
+    </build>
61
+
62
+</project>

+ 76
- 0
src/main/java/de/lusiardi/testprojekt/controller/RestController.java View File

@@ -0,0 +1,76 @@
1
+package de.lusiardi.testprojekt.controller;
2
+
3
+import de.lusiardi.testprojekt.dto.PersonCreateDto;
4
+import de.lusiardi.testprojekt.dto.PersonDto;
5
+import de.lusiardi.testprojekt.exceptions.ResourceNotFoundException;
6
+import java.util.ArrayList;
7
+import java.util.List;
8
+import org.springframework.http.HttpStatus;
9
+import org.springframework.stereotype.Controller;
10
+import org.springframework.web.bind.annotation.*;
11
+
12
+@Controller
13
+public class RestController {
14
+    
15
+    private List<PersonDto> persons = new ArrayList<PersonDto>();
16
+    
17
+    @RequestMapping(value = "persons", method = RequestMethod.GET)
18
+    @ResponseBody
19
+    public List<PersonDto> getPersons() {
20
+        return persons;
21
+    }
22
+    
23
+    @RequestMapping(value = "persons", method = RequestMethod.POST)
24
+    @ResponseBody
25
+    @ResponseStatus(HttpStatus.CREATED)
26
+    public PersonDto createPerson(@RequestBody PersonCreateDto in) {
27
+        PersonDto person = new PersonDto(in.getName(), in.getEmail());
28
+        persons.add(person);
29
+        return person;
30
+    }
31
+    
32
+    @RequestMapping(value = "persons/{id}", method = RequestMethod.GET)
33
+    @ResponseBody
34
+    public PersonDto getPerson(@PathVariable(value = "id") int id) {
35
+        for (PersonDto person : persons) {
36
+            if (id == person.getId()) {
37
+                return person;
38
+            }
39
+        }
40
+        throw new ResourceNotFoundException();
41
+    }
42
+    
43
+    @RequestMapping(value = "persons/{id}", method = RequestMethod.DELETE)
44
+    @ResponseBody
45
+    public boolean deletePerson(@PathVariable(value = "id") int id) {
46
+        PersonDto toDelete = null;
47
+        for (PersonDto person : persons) {
48
+            if (id == person.getId()) {
49
+                toDelete = person;
50
+            }
51
+        }
52
+        if (toDelete != null) {
53
+            return persons.remove(toDelete);
54
+        } else {
55
+            throw new ResourceNotFoundException();
56
+        }
57
+    }
58
+    
59
+    @RequestMapping(value = "persons/{id}", method = RequestMethod.PUT)
60
+    @ResponseBody
61
+    public PersonDto updatePerson(@PathVariable(value = "id") int id, @RequestBody PersonCreateDto in) {
62
+        PersonDto toUpdate = null;
63
+        for (PersonDto person : persons) {
64
+            if (id == person.getId()) {
65
+                toUpdate = person;
66
+            }
67
+        }
68
+        if (toUpdate != null) {
69
+            toUpdate.setEmail(in.getEmail());
70
+            toUpdate.setName(in.getName());
71
+            return toUpdate;
72
+        } else {
73
+            throw new ResourceNotFoundException();
74
+        }
75
+    }
76
+}

+ 66
- 0
src/main/java/de/lusiardi/testprojekt/dto/PersonCreateDto.java View File

@@ -0,0 +1,66 @@
1
+package de.lusiardi.testprojekt.dto;
2
+
3
+import javax.xml.bind.annotation.XmlRootElement;
4
+
5
+@XmlRootElement // for jaxb 
6
+public class PersonCreateDto {
7
+
8
+    private String name;
9
+    private String email;
10
+
11
+    /*
12
+     * de.lusiardi.testprojekt.dto.TestDto does not have a no-arg default 
13
+     * constructor. this problem is related to the following location: 
14
+     * at de.lusiardi.testprojekt.dto.TestDto
15
+     */
16
+    public PersonCreateDto() {
17
+    }
18
+
19
+    public PersonCreateDto(String name, String email) {
20
+        this.name = name;
21
+        this.email = email;
22
+    }
23
+
24
+    public String getName() {
25
+        return name;
26
+    }
27
+
28
+    public void setName(String foo) {
29
+        this.name = foo;
30
+    }
31
+
32
+    public String getEmail() {
33
+        return email;
34
+    }
35
+
36
+    public void setEmail(String email) {
37
+        this.email = email;
38
+    }
39
+
40
+    @Override
41
+    public int hashCode() {
42
+        int hash = 3;
43
+        hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
44
+        hash = 37 * hash + (this.email != null ? this.email.hashCode() : 0);
45
+        return hash;
46
+    }
47
+
48
+    @Override
49
+    public boolean equals(Object obj) {
50
+        if (obj == null) {
51
+            return false;
52
+        }
53
+        if (getClass() != obj.getClass()) {
54
+            return false;
55
+        }
56
+        final PersonCreateDto other = (PersonCreateDto) obj;
57
+        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
58
+            return false;
59
+        }
60
+        if ((this.email == null) ? (other.email != null) : !this.email.equals(other.email)) {
61
+            return false;
62
+        }
63
+        return true;
64
+    }
65
+
66
+}

+ 75
- 0
src/main/java/de/lusiardi/testprojekt/dto/PersonDto.java View File

@@ -0,0 +1,75 @@
1
+package de.lusiardi.testprojekt.dto;
2
+
3
+import javax.xml.bind.annotation.XmlRootElement;
4
+
5
+@XmlRootElement // for jaxb 
6
+public class PersonDto {
7
+
8
+    private static int idCounter = 0;
9
+    private int id;
10
+    private String name;
11
+    private String email;
12
+
13
+    /*
14
+     * de.lusiardi.testprojekt.dto.TestDto does not have a no-arg default 
15
+     * constructor. this problem is related to the following location: 
16
+     * at de.lusiardi.testprojekt.dto.TestDto
17
+     */
18
+    public PersonDto() {
19
+    }
20
+
21
+    public PersonDto(String name, String email) {
22
+        this.id = idCounter;
23
+        idCounter++;
24
+        this.name = name;
25
+        this.email = email;
26
+    }
27
+
28
+    public String getName() {
29
+        return name;
30
+    }
31
+
32
+    public void setName(String foo) {
33
+        this.name = foo;
34
+    }
35
+
36
+    public int getId() {
37
+        return id;
38
+    }
39
+
40
+    public void setId(int id) {
41
+        this.id = id;
42
+    }
43
+
44
+    public String getEmail() {
45
+        return email;
46
+    }
47
+
48
+    public void setEmail(String email) {
49
+        this.email = email;
50
+    }
51
+
52
+    @Override
53
+    public int hashCode() {
54
+        int hash = 7;
55
+        hash = 59 * hash + this.id;
56
+        hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
57
+        hash = 59 * hash + (this.email != null ? this.email.hashCode() : 0);
58
+        return hash;
59
+    }
60
+
61
+    @Override
62
+    public boolean equals(Object obj) {
63
+        if (obj == null) {
64
+            return false;
65
+        }
66
+        if (getClass() != obj.getClass()) {
67
+            return false;
68
+        }
69
+        final PersonDto other = (PersonDto) obj;
70
+        if (this.id != other.id) {
71
+            return false;
72
+        }
73
+        return true;
74
+    }
75
+}

+ 12
- 0
src/main/java/de/lusiardi/testprojekt/exceptions/ResourceNotFoundException.java View File

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

+ 2
- 0
src/main/webapp/META-INF/context.xml View File

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

+ 30
- 0
src/main/webapp/WEB-INF/spring-servlet.xml View File

@@ -0,0 +1,30 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<beans xmlns="http://www.springframework.org/schema/beans"
3
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+       xmlns:mvc="http://www.springframework.org/schema/mvc"
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7
+       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
8
+       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9
+
10
+    <!--
11
+        beans für das mapping von json und xml
12
+    -->
13
+    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
14
+    <bean id="jaxbMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
15
+    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
16
+        <property name="messageConverters">
17
+            <list>
18
+                <ref bean="jacksonMessageConverter"/>
19
+                <ref bean="jaxbMessageConverter"/>
20
+            </list>
21
+        </property>
22
+    </bean>
23
+
24
+    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
25
+    
26
+    <context:component-scan base-package="de.lusiardi.testprojekt" />
27
+    
28
+    <mvc:annotation-driven />
29
+
30
+</beans>

+ 28
- 0
src/main/webapp/WEB-INF/web.xml View File

@@ -0,0 +1,28 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<web-app version="3.0" 
3
+         xmlns="http://java.sun.com/xml/ns/javaee" 
4
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5
+         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
6
+        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7
+
8
+    <display-name>WebBasics3</display-name>
9
+
10
+    <welcome-file-list>
11
+        <welcome-file>index.jsp</welcome-file>
12
+    </welcome-file-list>   
13
+
14
+    <servlet>
15
+        <servlet-name>spring</servlet-name>
16
+        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
17
+        <load-on-startup>1</load-on-startup>
18
+    </servlet>
19
+
20
+    <!--
21
+        all restfull urls go here
22
+    -->
23
+    <servlet-mapping>
24
+        <servlet-name>spring</servlet-name>
25
+        <url-pattern>/rest/*</url-pattern>
26
+    </servlet-mapping>
27
+
28
+</web-app>

+ 12
- 0
src/main/webapp/index.jsp View File

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