extract spring-mvc-basics-2 module
This commit is contained in:
parent
e848a0ffcf
commit
5182035762
@ -15,14 +15,11 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
|||||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||||
- [What’s New in Spring 4.3?](http://www.baeldung.com/whats-new-in-spring-4-3)
|
- [What’s New in Spring 4.3?](http://www.baeldung.com/whats-new-in-spring-4-3)
|
||||||
- [Running Setup Data on Startup in Spring](http://www.baeldung.com/running-setup-logic-on-startup-in-spring)
|
- [Running Setup Data on Startup in Spring](http://www.baeldung.com/running-setup-logic-on-startup-in-spring)
|
||||||
- [Quick Guide to Spring Controllers](http://www.baeldung.com/spring-controllers)
|
|
||||||
- [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes)
|
- [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes)
|
||||||
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
||||||
- [A CLI with Spring Shell](http://www.baeldung.com/spring-shell-cli)
|
- [A CLI with Spring Shell](http://www.baeldung.com/spring-shell-cli)
|
||||||
- [JasperReports with Spring](http://www.baeldung.com/spring-jasper)
|
- [JasperReports with Spring](http://www.baeldung.com/spring-jasper)
|
||||||
- [Model, ModelMap, and ModelView in Spring MVC](http://www.baeldung.com/spring-mvc-model-model-map-model-view)
|
|
||||||
- [@Order in Spring](http://www.baeldung.com/spring-order)
|
- [@Order in Spring](http://www.baeldung.com/spring-order)
|
||||||
- [Spring Web Contexts](http://www.baeldung.com/spring-web-contexts)
|
|
||||||
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
|
- [Spring @Primary Annotation](http://www.baeldung.com/spring-primary)
|
||||||
- [Spring Events](https://www.baeldung.com/spring-events)
|
- [Spring Events](https://www.baeldung.com/spring-events)
|
||||||
- [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations)
|
- [Spring Null-Safety Annotations](https://www.baeldung.com/spring-null-safety-annotations)
|
||||||
|
13
spring-mvc-basics-2/.gitignore
vendored
Normal file
13
spring-mvc-basics-2/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
*.class
|
||||||
|
|
||||||
|
#folders#
|
||||||
|
/target
|
||||||
|
/neoDb*
|
||||||
|
/data
|
||||||
|
/src/main/webapp/WEB-INF/classes
|
||||||
|
*/META-INF/*
|
||||||
|
|
||||||
|
# Packaged files #
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.ear
|
11
spring-mvc-basics-2/README.md
Normal file
11
spring-mvc-basics-2/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Spring MVC Basics with Java Configuration Example Project
|
||||||
|
|
||||||
|
### The Course
|
||||||
|
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Quick Guide to Spring Controllers](http://www.baeldung.com/spring-controllers)- [The Spring @Controller and @RestController Annotations](http://www.baeldung.com/spring-controller-vs-restcontroller)
|
||||||
|
- [Model, ModelMap, and ModelView in Spring MVC](http://www.baeldung.com/spring-mvc-model-model-map-model-view)
|
||||||
|
- [Spring Web Contexts](http://www.baeldung.com/spring-web-contexts)
|
37
spring-mvc-basics-2/pom.xml
Normal file
37
spring-mvc-basics-2/pom.xml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<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>
|
||||||
|
<artifactId>spring-mvc-basics-2</artifactId>
|
||||||
|
<name>spring-mvc-basics-2</name>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
@ -2,7 +2,6 @@ package com.baeldung.contexts.config;
|
|||||||
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
|
||||||
|
|
||||||
public class AnnotationsBasedApplicationAndServletInitializer //extends AbstractDispatcherServletInitializer
|
public class AnnotationsBasedApplicationAndServletInitializer //extends AbstractDispatcherServletInitializer
|
||||||
{
|
{
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.contexts.config;
|
package com.baeldung.contexts.config;
|
||||||
|
|
||||||
import org.springframework.web.context.AbstractContextLoaderInitializer;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
|
@ -2,7 +2,6 @@ package com.baeldung.contexts.config;
|
|||||||
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
|
|
||||||
|
|
||||||
public class SecureAnnotationsBasedApplicationAndServletInitializer// extends AbstractDispatcherServletInitializer
|
public class SecureAnnotationsBasedApplicationAndServletInitializer// extends AbstractDispatcherServletInitializer
|
||||||
{
|
{
|
@ -1,32 +1,28 @@
|
|||||||
package org.baeldung.controller.config;
|
package org.baeldung.controller.config;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletRegistration;
|
import javax.servlet.ServletRegistration;
|
||||||
|
|
||||||
import org.springframework.context.support.GenericApplicationContext;
|
import org.springframework.web.context.ContextLoaderListener;
|
||||||
import org.springframework.web.WebApplicationInitializer;
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
import org.springframework.web.context.ContextLoaderListener;
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
|
||||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
public class StudentControllerConfig //implements WebApplicationInitializer
|
||||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
{
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
|
||||||
|
//uncomment to run the student controller example
|
||||||
public class StudentControllerConfig //implements WebApplicationInitializer
|
//@Override
|
||||||
{
|
public void onStartup(ServletContext sc) throws ServletException {
|
||||||
|
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||||
//uncomment to run the student controller example
|
root.register(WebConfig.class);
|
||||||
//@Override
|
root.setServletContext(sc);
|
||||||
public void onStartup(ServletContext sc) throws ServletException {
|
sc.addListener(new ContextLoaderListener(root));
|
||||||
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
|
||||||
root.register(WebConfig.class);
|
DispatcherServlet dv = new DispatcherServlet(root);
|
||||||
root.setServletContext(sc);
|
|
||||||
sc.addListener(new ContextLoaderListener(root));
|
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
|
||||||
|
appServlet.setLoadOnStartup(1);
|
||||||
DispatcherServlet dv = new DispatcherServlet(root);
|
appServlet.addMapping("/test/*");
|
||||||
|
}
|
||||||
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
|
}
|
||||||
appServlet.setLoadOnStartup(1);
|
|
||||||
appServlet.addMapping("/test/*");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +1,28 @@
|
|||||||
package org.baeldung.controller.config;
|
package org.baeldung.controller.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "org.baeldung.controller.config" })
|
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "org.baeldung.controller.config" })
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
@Override
|
@Override
|
||||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||||
configurer.enable();
|
configurer.enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ViewResolver viewResolver() {
|
public ViewResolver viewResolver() {
|
||||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||||
bean.setPrefix("/WEB-INF/");
|
bean.setPrefix("/WEB-INF/");
|
||||||
bean.setSuffix(".jsp");
|
bean.setSuffix(".jsp");
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,8 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
public class RestController {
|
public class RestController {
|
||||||
|
|
||||||
@GetMapping(value = "/student/{studentId}")
|
@GetMapping(value = "/student/{studentId}")
|
||||||
public @ResponseBody Student getTestData(@PathVariable Integer studentId) {
|
public @ResponseBody
|
||||||
|
Student getTestData(@PathVariable Integer studentId) {
|
||||||
Student student = new Student();
|
Student student = new Student();
|
||||||
student.setName("Peter");
|
student.setName("Peter");
|
||||||
student.setId(studentId);
|
student.setId(studentId);
|
@ -0,0 +1 @@
|
|||||||
|
spring.main.allow-bean-definition-overriding=true
|
13
spring-mvc-basics-2/src/main/resources/logback.xml
Normal file
13
spring-mvc-basics-2/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
@ -1,79 +1,79 @@
|
|||||||
package org.baeldung.controller;
|
package org.baeldung.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.baeldung.controller.config.WebConfig;
|
import org.baeldung.controller.config.WebConfig;
|
||||||
import org.baeldung.controller.student.Student;
|
import org.baeldung.controller.student.Student;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
|
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
@ContextConfiguration(classes = { WebConfig.class }, loader = AnnotationConfigWebContextLoader.class)
|
@ContextConfiguration(classes = { WebConfig.class }, loader = AnnotationConfigWebContextLoader.class)
|
||||||
public class ControllerAnnotationIntegrationTest {
|
public class ControllerAnnotationIntegrationTest {
|
||||||
|
|
||||||
private MockMvc mockMvc;
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext wac;
|
private WebApplicationContext wac;
|
||||||
|
|
||||||
private Student selectedStudent;
|
private Student selectedStudent;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||||
|
|
||||||
selectedStudent = new Student();
|
selectedStudent = new Student();
|
||||||
selectedStudent.setId(1);
|
selectedStudent.setId(1);
|
||||||
selectedStudent.setName("Peter");
|
selectedStudent.setName("Peter");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTestController() throws Exception {
|
public void testTestController() throws Exception {
|
||||||
|
|
||||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/test/")).andReturn().getModelAndView();
|
||||||
|
|
||||||
// validate modal data
|
// validate modal data
|
||||||
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
Assert.assertSame(mv.getModelMap().get("data").toString(), "Welcome home man");
|
||||||
|
|
||||||
// validate view name
|
// validate view name
|
||||||
Assert.assertSame(mv.getViewName(), "welcome");
|
Assert.assertSame(mv.getViewName(), "welcome");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRestController() throws Exception {
|
public void testRestController() throws Exception {
|
||||||
|
|
||||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||||
|
|
||||||
ObjectMapper reader = new ObjectMapper();
|
ObjectMapper reader = new ObjectMapper();
|
||||||
|
|
||||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||||
|
|
||||||
Assert.assertEquals(selectedStudent, studentDetails);
|
Assert.assertEquals(selectedStudent, studentDetails);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRestAnnotatedController() throws Exception {
|
public void testRestAnnotatedController() throws Exception {
|
||||||
|
|
||||||
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
String responseBody = this.mockMvc.perform(MockMvcRequestBuilders.get("/annotated/student/{studentId}", 1)).andReturn().getResponse().getContentAsString();
|
||||||
|
|
||||||
ObjectMapper reader = new ObjectMapper();
|
ObjectMapper reader = new ObjectMapper();
|
||||||
|
|
||||||
Student studentDetails = reader.readValue(responseBody, Student.class);
|
Student studentDetails = reader.readValue(responseBody, Student.class);
|
||||||
|
|
||||||
Assert.assertEquals(selectedStudent, studentDetails);
|
Assert.assertEquals(selectedStudent, studentDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
24
spring-mvc-basics-2/src/test/resources/test-mvc.xml
Normal file
24
spring-mvc-basics-2/src/test/resources/test-mvc.xml
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?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="org.baeldung.controller.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>
|
Loading…
x
Reference in New Issue
Block a user