Refactor spring-controller

This commit is contained in:
Grzegorz Piwowarek 2016-07-14 23:05:25 +03:00
parent 0f6218ec91
commit 2113974e1c
17 changed files with 296 additions and 426 deletions

View File

@ -42,6 +42,7 @@
<module>spring-apache-camel</module> <module>spring-apache-camel</module>
<module>spring-batch</module> <module>spring-batch</module>
<module>spring-boot</module> <module>spring-boot</module>
<module>spring-controller</module>
<module>spring-data-cassandra</module> <module>spring-data-cassandra</module>
<module>spring-data-elasticsearch</module> <module>spring-data-elasticsearch</module>
<module>spring-data-mongodb</module> <module>spring-data-mongodb</module>

View File

@ -1,31 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>spring-batch</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -1,56 +1,56 @@
<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"> <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> <modelVersion>4.0.0</modelVersion>
<groupId>test</groupId> <groupId>test</groupId>
<artifactId>test-mvc</artifactId> <artifactId>spring-controller</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version> <version>4.3.0.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version> <version>3.0.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> <artifactId>jackson-databind</artifactId>
<version>2.6.3</version> <version>2.6.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId> <artifactId>jackson-annotations</artifactId>
<version>2.6.3</version> <version>2.6.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId> <artifactId>jackson-core</artifactId>
<version>2.6.3</version> <version>2.6.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
<version>4.3.0.RELEASE</version> <version>4.3.0.RELEASE</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>4.12</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version> <version>4.2.6.RELEASE</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,19 @@
package com.baledung.controller;
import com.baledung.student.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestAnnotatedController {
@GetMapping(value = "/annotated/student/{studentId}")
public Student getData(@PathVariable Integer studentId) {
Student student = new Student();
student.setName("Peter");
student.setId(studentId);
return student;
}
}

View File

@ -1,30 +1,21 @@
package com.baledung.controller; package com.baledung.controller;
import org.springframework.stereotype.Controller; import com.baledung.student.Student;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.baledung.student.Student; @Controller
public class RestController{
@Controller
public class RestController{ @GetMapping(value="/student/{studentId}")
public @ResponseBody Student getTestData(@PathVariable Integer studentId) {
/** Student student = new Student();
* Get a student based on the id of the student student.setName("Peter");
* specified in path variable {studentId} student.setId(studentId);
* @param studentId
* @return return student;
*/
@RequestMapping(value="/student/{studentId}",method=RequestMethod.GET) }
public @ResponseBody Student getTestData(@PathVariable Integer studentId) }
{
Student student = new Student();
student.setName("Peter");
student.setId(studentId);
return student;
}
}

View File

@ -0,0 +1,24 @@
/**
* @author Prashant Dutta
*/
package com.baledung.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = "/test")
public class TestController {
@GetMapping
public ModelAndView getTestData() {
ModelAndView mv = new ModelAndView();
mv.setViewName("welcome");
mv.getModel().put("data", "Welcome home man");
return mv;
}
}

View File

@ -1,40 +1,33 @@
/** package com.baledung.student;
*
*/ public class Student {
package com.baledung.student; private String name;
public class Student private int id;
{
private String name; public String getName() {
return name;
private int id; }
//----------------------------- getters and setters-------------------------------------------------------- public void setName(String name) {
public String getName() { this.name = name;
return name; }
}
public int getId() {
public void setName(String name) { return id;
this.name = name; }
}
public void setId(int id) {
public int getId() { this.id = id;
return id; }
}
@Override
public void setId(int id) { public int hashCode(){
this.id = id; return this.id;
} }
@Override @Override
public int hashCode(){ public boolean equals(Object obj){
return this.id; return this.name.equals(((Student)obj).getName());
} }
@Override
public boolean equals(Object obj){
return this.name.equals(((Student)obj).getName());
}
} }

View File

@ -1,24 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation=" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.baledung.controller" /> <context:component-scan base-package="com.baledung.controller" />
<mvc:annotation-driven /> <mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"> <property name="prefix">
<value>/WEB-INF/</value> <value>/WEB-INF/</value>
</property> </property>
<property name="suffix"> <property name="suffix">
<value>.jsp</value> <value>.jsp</value>
</property> </property>
</bean> </bean>
</beans> </beans>

View File

@ -0,0 +1,27 @@
<web-app
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"
version="3.0">
<servlet>
<servlet-name>test-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test-mvc</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

View File

@ -1,12 +1,12 @@
<%@ page language="java" contentType="text/html; charset=UTF-8" <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> <title>Insert title here</title>
</head> </head>
<body> <body>
Data returned is ${data} Data returned is ${data}
</body> </body>
</html> </html>

View File

@ -0,0 +1,83 @@
package com.baledung.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import com.baledung.student.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:test-mvc.xml"})
public class ControllerTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
private Student selectedStudent;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
selectedStudent = new Student();
selectedStudent.setId(1);
selectedStudent.setName("Peter");
}
@Test
public void testTestController() throws Exception {
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/"))
.andReturn()
.getModelAndView();
// validate modal data
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
// validate view name
Assert.assertSame(mv.getViewName(), "welcome");
}
@Test
public void testRestController() throws Exception {
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1))
.andReturn().getResponse()
.getContentAsString();
ObjectMapper reader = new ObjectMapper();
Student studentDetails = reader.readValue(responseBody, Student.class);
Assert.assertEquals(selectedStudent, studentDetails);
}
@Test
public void testRestAnnotatedController() throws Exception {
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1))
.andReturn().getResponse()
.getContentAsString();
ObjectMapper reader = new ObjectMapper();
Student studentDetails = reader.readValue(responseBody, Student.class);
Assert.assertEquals(selectedStudent, studentDetails);
}
}

View File

@ -1,21 +0,0 @@
package com.baledung.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baledung.student.Student;
@RestController
public class RestAnnotatedController
{
@RequestMapping(value="/annotated/student/{studentId}")
public Student getData(@PathVariable Integer studentId)
{
Student student = new Student();
student.setName("Peter");
student.setId(studentId);
return student;
}
}

View File

@ -1,26 +0,0 @@
/**
* @author Prashant Dutta
*
*/
package com.baledung.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/test")
public class TestController{
@RequestMapping(method=RequestMethod.GET)
public ModelAndView getTestData()
{
ModelAndView mv = new ModelAndView();
mv.setViewName("welcome");
mv.getModel().put("data", "Welcome home man");
return mv;
}
}

View File

@ -1,24 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.test.controller" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

View File

@ -1,32 +0,0 @@
<web-app
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"
version="3.0">
<servlet>
<servlet-name>test-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test-mvc</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

View File

@ -1,105 +0,0 @@
package com.baledung.test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import com.baledung.student.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* <p>
* Test class for spring controllers
* </p>
* @author Prashant.Dutta
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:test-mvc.xml"})
public class ControllerTest
{
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
private Student selectedStudent;
@Before
public void setUp()
{
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
selectedStudent = new Student();
selectedStudent.setId(1);
selectedStudent.setName("Peter");
}
/**
* Test basic test controller
* @throws Exception
*/
@Test
public void testTestController() throws Exception
{
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/"))
.andReturn()
.getModelAndView();
// validate modal data
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
// validate view name
Assert.assertSame(mv.getViewName(), "welcome");
}
/**
* Test rest controller
* @throws Exception
*/
@Test
public void testRestController() throws Exception
{
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}",1))
.andReturn().getResponse()
.getContentAsString();
ObjectMapper reader = new ObjectMapper();
Student studentDetails = reader.readValue(responseBody, Student.class);
Assert.assertEquals(selectedStudent, studentDetails);
}
/**
* Test rest controller annotated with test
* @throws Exception
*/
@Test
public void testRestAnnotatedController() throws Exception
{
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}",1))
.andReturn().getResponse()
.getContentAsString();
ObjectMapper reader = new ObjectMapper();
Student studentDetails = reader.readValue(responseBody, Student.class);
Assert.assertEquals(selectedStudent, studentDetails);
}
}