[BAEL-19881] - Rename spring-mvc-simple modules
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.contexts;
|
||||
|
||||
public class Greeting {
|
||||
|
||||
private String message;
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Greeting [message=" + message + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class AnnotationsBasedApplicationAndServletInitializer //extends AbstractDispatcherServletInitializer
|
||||
{
|
||||
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
//If this is not the only class declaring a root context, we return null because it would clash
|
||||
//with other classes, as there can only be a single root context.
|
||||
|
||||
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
//rootContext.register(RootApplicationConfig.class);
|
||||
//return rootContext;
|
||||
return null;
|
||||
}
|
||||
|
||||
//@Override
|
||||
protected WebApplicationContext createServletApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext normalWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
normalWebAppContext.register(NormalWebAppConfig.class);
|
||||
return normalWebAppContext;
|
||||
}
|
||||
|
||||
//@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/api/*" };
|
||||
}
|
||||
|
||||
//@Override
|
||||
protected String getServletName() {
|
||||
return "normal-dispatcher";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class AnnotationsBasedApplicationInitializer //extends AbstractContextLoaderInitializer
|
||||
{
|
||||
//uncomment to run the multiple contexts example
|
||||
// @Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
rootContext.register(RootApplicationConfig.class);
|
||||
return rootContext;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.context.support.XmlWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class ApplicationInitializer //implements WebApplicationInitializer
|
||||
{
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
//Here, we can define a root context and register servlets, among other things.
|
||||
//However, since we've later defined other classes to do the same and they would clash,
|
||||
//we leave this commented out.
|
||||
|
||||
//Root XML Context
|
||||
//XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
|
||||
//rootContext.setConfigLocations("/WEB-INF/rootApplicationContext.xml");
|
||||
//Annotations Context
|
||||
//AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
//rootContext.register(RootApplicationConfig.class);
|
||||
//Registration
|
||||
//servletContext.addListener(new ContextLoaderListener(rootContext));
|
||||
|
||||
//Dispatcher Servlet
|
||||
//XmlWebApplicationContext normalWebAppContext = new XmlWebApplicationContext();
|
||||
//normalWebAppContext.setConfigLocation("/WEB-INF/normal-webapp-servlet.xml");
|
||||
//ServletRegistration.Dynamic normal = servletContext.addServlet("normal-webapp", new DispatcherServlet(normalWebAppContext));
|
||||
//normal.setLoadOnStartup(1);
|
||||
//normal.addMapping("/api/*");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.contexts.normal" })
|
||||
public class NormalWebAppConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
resolver.setPrefix("/WEB-INF/view/");
|
||||
resolver.setSuffix(".jsp");
|
||||
resolver.setViewClass(JstlView.class);
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.contexts.Greeting;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.contexts.services" })
|
||||
public class RootApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public Greeting greeting() {
|
||||
Greeting greeting = new Greeting();
|
||||
greeting.setMessage("Hello World !!");
|
||||
return greeting;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
public class SecureAnnotationsBasedApplicationAndServletInitializer// extends AbstractDispatcherServletInitializer
|
||||
{
|
||||
|
||||
//uncomment to run the multiple contexts example
|
||||
//@Override
|
||||
protected WebApplicationContext createRootApplicationContext() {
|
||||
return null;
|
||||
}
|
||||
|
||||
//@Override
|
||||
protected WebApplicationContext createServletApplicationContext() {
|
||||
AnnotationConfigWebApplicationContext secureWebAppContext = new AnnotationConfigWebApplicationContext();
|
||||
secureWebAppContext.register(SecureWebAppConfig.class);
|
||||
return secureWebAppContext;
|
||||
}
|
||||
|
||||
//@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/s/api/*" };
|
||||
}
|
||||
|
||||
|
||||
//@Override
|
||||
protected String getServletName() {
|
||||
return "secure-dispatcher";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.contexts.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "com.baeldung.contexts.secure" })
|
||||
public class SecureWebAppConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
resolver.setPrefix("/WEB-INF/secure/view/");
|
||||
resolver.setSuffix(".jsp");
|
||||
resolver.setViewClass(JstlView.class);
|
||||
return resolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.contexts.normal;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.contexts.services.GreeterService;
|
||||
|
||||
@Controller
|
||||
public class HelloWorldController {
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext webApplicationContext;
|
||||
|
||||
@Autowired
|
||||
private GreeterService greeterService;
|
||||
|
||||
private void processContext() {
|
||||
WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext();
|
||||
|
||||
System.out.println("root context : " + rootContext);
|
||||
System.out.println("root context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames()));
|
||||
|
||||
System.out.println("context : " + webApplicationContext);
|
||||
System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames()));
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/welcome")
|
||||
public ModelAndView helloWorld() {
|
||||
processContext();
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3>Normal " + greeterService.greet() + "</h3></div>";
|
||||
return new ModelAndView("welcome", "message", message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.contexts.secure;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.contexts.services.ApplicationContextUtilService;
|
||||
import com.baeldung.contexts.services.GreeterService;
|
||||
|
||||
@Controller
|
||||
public class HelloWorldSecureController {
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext webApplicationContext;
|
||||
|
||||
@Autowired
|
||||
private GreeterService greeterService;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("contextAware")
|
||||
private ApplicationContextUtilService contextUtilService;
|
||||
|
||||
private void processContext() {
|
||||
ApplicationContext context = contextUtilService.getApplicationContext();
|
||||
System.out.println("application context : " + context);
|
||||
System.out.println("application context Beans: " + Arrays.asList(context.getBeanDefinitionNames()));
|
||||
|
||||
WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext();
|
||||
System.out.println("context : " + rootContext);
|
||||
System.out.println("context Beans: " + Arrays.asList(rootContext.getBeanDefinitionNames()));
|
||||
|
||||
System.out.println("context : " + webApplicationContext);
|
||||
System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames()));
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/welcome")
|
||||
public ModelAndView helloWorld() {
|
||||
processContext();
|
||||
String message = "<br><div style='text-align:center;'>" + "<h3>Secure " + greeterService.greet() + "</h3></div>";
|
||||
return new ModelAndView("welcome", "message", message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.contexts.services;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service(value="contextAware")
|
||||
public class ApplicationContextUtilService implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.contexts.services;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.contexts.Greeting;
|
||||
|
||||
@Service
|
||||
public class GreeterService {
|
||||
|
||||
@Resource
|
||||
private Greeting greeting;
|
||||
|
||||
public String greet(){
|
||||
return greeting.getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.baeldung.controller.config;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
public class StudentControllerConfig //implements WebApplicationInitializer
|
||||
{
|
||||
|
||||
//uncomment to run the student controller example
|
||||
//@Override
|
||||
public void onStartup(ServletContext sc) throws ServletException {
|
||||
AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
|
||||
root.register(WebConfig.class);
|
||||
root.setServletContext(sc);
|
||||
sc.addListener(new ContextLoaderListener(root));
|
||||
|
||||
DispatcherServlet dv = new DispatcherServlet(root);
|
||||
|
||||
ServletRegistration.Dynamic appServlet = sc.addServlet("test-mvc", dv);
|
||||
appServlet.setLoadOnStartup(1);
|
||||
appServlet.addMapping("/test/*");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.baeldung.controller.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = { "org.baeldung.controller.controller", "com.baeldung.controller", "org.baeldung.controller.config" })
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setPrefix("/WEB-INF/");
|
||||
bean.setSuffix(".jsp");
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.baeldung.controller.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* In this controller, Model, ModelMap and ModelAndView are shown as examples.
|
||||
* They are all used to pass parameters to JSP pages.
|
||||
* 04/09/2017
|
||||
*
|
||||
* @author Ahmet Cetin
|
||||
*/
|
||||
@Controller
|
||||
public class PassParametersController {
|
||||
@GetMapping("/showViewPage")
|
||||
public String passParametersWithModel(Model model) {
|
||||
model.addAttribute("message", "Baeldung");
|
||||
return "viewPage";
|
||||
}
|
||||
|
||||
@GetMapping("/printViewPage")
|
||||
public String passParametersWithModelMap(ModelMap map) {
|
||||
map.addAttribute("welcomeMessage", "welcome");
|
||||
map.addAttribute("message", "Baeldung");
|
||||
return "viewPage";
|
||||
}
|
||||
|
||||
@GetMapping("/goToViewPage")
|
||||
public ModelAndView passParametersWithModelAndView() {
|
||||
ModelAndView modelAndView = new ModelAndView("viewPage");
|
||||
modelAndView.addObject("message", "Baeldung");
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.controller.controller;
|
||||
|
||||
import org.baeldung.controller.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.baeldung.controller.controller;
|
||||
|
||||
import org.baeldung.controller.student.Student;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class RestController {
|
||||
|
||||
@GetMapping(value = "/student/{studentId}")
|
||||
public @ResponseBody
|
||||
Student getTestData(@PathVariable Integer studentId) {
|
||||
Student student = new Student();
|
||||
student.setName("Peter");
|
||||
student.setId(studentId);
|
||||
|
||||
return student;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
/**
|
||||
* @author Prashant Dutta
|
||||
*/
|
||||
package org.baeldung.controller.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.baeldung.controller.student;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
|
||||
private int id;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this.name.equals(((Student) obj).getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
public class Article {
|
||||
|
||||
public static final Article DEFAULT_ARTICLE = new Article(12);
|
||||
|
||||
private Integer id;
|
||||
|
||||
public Article(Integer articleId) {
|
||||
this.id = articleId;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Article [id=" + id + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class ArticleViewerController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/mapParam")
|
||||
public class ArticleViewerWithMapParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable Map<String, String> pathVarsMap) {
|
||||
|
||||
String articleId = pathVarsMap.get("id");
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(Integer.valueOf(articleId));
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/optionalParam")
|
||||
public class ArticleViewerWithOptionalParamController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id") Optional<Integer> optionalArticleId) {
|
||||
|
||||
if(optionalArticleId.isPresent()) {
|
||||
Integer articleId = optionalArticleId.get();
|
||||
return new Article(articleId);
|
||||
}else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/requiredAttribute")
|
||||
public class ArticleViewerWithRequiredAttributeController {
|
||||
|
||||
@RequestMapping(value = {"/article", "/article/{id}"})
|
||||
public Article getArticle(@PathVariable(name = "id", required = false) Integer articleId) {
|
||||
|
||||
if (articleId != null) {
|
||||
return new Article(articleId);
|
||||
} else {
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE;
|
||||
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/seperateMethods")
|
||||
public class ArticleViewerWithTwoSeparateMethodsController {
|
||||
|
||||
@RequestMapping(value = "/article/{id}")
|
||||
public Article getArticle(@PathVariable(name = "id") Integer articleId) {
|
||||
|
||||
return new Article(articleId);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/article")
|
||||
public Article getDefaultArticle() {
|
||||
|
||||
return DEFAULT_ARTICLE;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
13
spring-mvc-basics-4/src/main/resources/logback.xml
Normal file
13
spring-mvc-basics-4/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>
|
||||
24
spring-mvc-basics-4/src/main/resources/test-mvc.xml
Normal file
24
spring-mvc-basics-4/src/main/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>
|
||||
11
spring-mvc-basics-4/src/main/webapp/WEB-INF/greeting.xml
Normal file
11
spring-mvc-basics-4/src/main/webapp/WEB-INF/greeting.xml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version = "1.0" encoding = "UTF-8"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="greeting" class="com.baeldung.contexts.Greeting">
|
||||
<property name="message" value="Hello World !!"/>
|
||||
</bean>
|
||||
</beans>
|
||||
5
spring-mvc-basics-4/src/main/webapp/WEB-INF/index.jsp
Normal file
5
spring-mvc-basics-4/src/main/webapp/WEB-INF/index.jsp
Normal file
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h2>Hello World!</h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,16 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.contexts.normal"/>
|
||||
|
||||
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
|
||||
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
|
||||
<property name="prefix" value="/WEB-INF/view/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?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:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.contexts.services"/>
|
||||
|
||||
<import resource="greeting.xml"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,16 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.contexts.secure"/>
|
||||
|
||||
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
|
||||
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
|
||||
<property name="prefix" value="/WEB-INF/secure/view/"/>
|
||||
<property name="suffix" value=".jsp"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring Web Contexts</title>
|
||||
</head>
|
||||
<body>
|
||||
<br>
|
||||
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
|
||||
Secure Web Application : ${message}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>This is the body of the sample view</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>Bean Scopes Examples</h1>
|
||||
<br> Previous Message: ${previousMessage }
|
||||
<br> Current Message: ${currentMessage }
|
||||
<br>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>Web Application. Passed parameter : th:text="${message}"</div>
|
||||
</body>
|
||||
</html>
|
||||
11
spring-mvc-basics-4/src/main/webapp/WEB-INF/view/welcome.jsp
Normal file
11
spring-mvc-basics-4/src/main/webapp/WEB-INF/view/welcome.jsp
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring Web Contexts</title>
|
||||
</head>
|
||||
<body>
|
||||
<br>
|
||||
<div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;">
|
||||
Normal Web Application : ${message}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
88
spring-mvc-basics-4/src/main/webapp/WEB-INF/web-old.xml
Normal file
88
spring-mvc-basics-4/src/main/webapp/WEB-INF/web-old.xml
Normal file
@@ -0,0 +1,88 @@
|
||||
<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">
|
||||
|
||||
<!-- Uncommented, this disallows org.springframework.web.SpringServletContainerInitializer to run and execute
|
||||
application initializers. -->
|
||||
<!--<absolute-ordering>
|
||||
</absolute-ordering>-->
|
||||
|
||||
<!-- root application context -->
|
||||
<!--<listener>
|
||||
<listener-class>
|
||||
org.springframework.web.context.ContextLoaderListener
|
||||
</listener-class>
|
||||
</listener>-->
|
||||
<!--<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/rootApplicationContext.xml</param-value>
|
||||
</context-param>-->
|
||||
<!--<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>com.baeldung.contexts.config.RootApplicationConfig, com.baeldung.contexts.config.NormalWebAppConfig</param-value>
|
||||
</context-param>-->
|
||||
<!--<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.bean.config</param-value>
|
||||
</context-param>-->
|
||||
|
||||
<!-- secure web app context -->
|
||||
<!--<servlet>
|
||||
<servlet-name>secure-webapp</servlet-name>
|
||||
<servlet-class>
|
||||
org.springframework.web.servlet.DispatcherServlet
|
||||
</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/secure-webapp-servlet.xml</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>secure-webapp</servlet-name>
|
||||
<url-pattern>/s/api/*</url-pattern>
|
||||
</servlet-mapping>-->
|
||||
|
||||
<!-- normal web app context -->
|
||||
<!--<servlet>
|
||||
<servlet-name>normal-webapp</servlet-name>
|
||||
<servlet-class>
|
||||
org.springframework.web.servlet.DispatcherServlet
|
||||
</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>normal-webapp</servlet-name>
|
||||
<url-pattern>/api/*</url-pattern>
|
||||
</servlet-mapping>-->
|
||||
<!-- normal webapp with annotations-based context -->
|
||||
<servlet>
|
||||
<servlet-name>normal-webapp-annotations</servlet-name>
|
||||
<servlet-class>
|
||||
org.springframework.web.servlet.DispatcherServlet
|
||||
</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>com.baeldung.contexts.config.NormalWebAppConfig</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>normal-webapp-annotations</servlet-name>
|
||||
<url-pattern>/api-ann/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>/WEB-INF/index.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
||||
12
spring-mvc-basics-4/src/main/webapp/WEB-INF/welcome.jsp
Normal file
12
spring-mvc-basics-4/src/main/webapp/WEB-INF/welcome.jsp
Normal file
@@ -0,0 +1,12 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
pageEncoding="UTF-8" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Insert title here</title>
|
||||
</head>
|
||||
<body>
|
||||
Data returned is ${data}
|
||||
</body>
|
||||
</html>
|
||||
5
spring-mvc-basics-4/src/main/webapp/index.jsp
Normal file
5
spring-mvc-basics-4/src/main/webapp/index.jsp
Normal file
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h2>Hello World!</h2>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,79 @@
|
||||
package org.baeldung.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
import org.baeldung.controller.student.Student;
|
||||
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.AnnotationConfigWebContextLoader;
|
||||
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;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class }, loader = AnnotationConfigWebContextLoader.class)
|
||||
public class ControllerAnnotationIntegrationTest {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package org.baeldung.controller;
|
||||
|
||||
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 org.baeldung.controller.student.Student;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration({ "classpath:test-mvc.xml" })
|
||||
public class ControllerIntegrationTest {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package org.baeldung.controller;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* This is the test class for {@link org.baeldung.controller.controller.PassParametersController} class.
|
||||
* 09/09/2017
|
||||
*
|
||||
* @author Ahmet Cetin
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration({"classpath:test-mvc.xml"})
|
||||
public class PassParametersControllerIntegrationTest {
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModel() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/showViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelMap() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/printViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassParametersWithModelAndView() throws Exception {
|
||||
ModelAndView mv = this.mockMvc.perform(MockMvcRequestBuilders.get("/goToViewPage")).andReturn().getModelAndView();
|
||||
|
||||
//Validate view
|
||||
Assert.assertEquals(mv.getViewName(), "viewPage");
|
||||
|
||||
//Validate attribute
|
||||
Assert.assertEquals(mv.getModelMap().get("message").toString(), "Baeldung");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
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.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIdPathVariableIsNotPassed_thenResponse500() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isInternalServerError());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
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.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithOptionalParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOPtionalParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/optionalParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
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.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerControllerWithRequiredAttributeIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 154;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRequiredAttributeIsFalse_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/requiredAttribute/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
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.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithMapParamIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPathVarsMapParam_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/mapParam/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.controller.optionalpathvars;
|
||||
|
||||
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.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.baeldung.controller.config.WebConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebConfig.class })
|
||||
public class ArticleViewerWithTwoSeparateMethodsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsPassed_thenResponseOK() throws Exception {
|
||||
|
||||
int articleId = 5;
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article/{id}", articleId))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSeparateMethods_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception {
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get("/seperateMethods/article"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
24
spring-mvc-basics-4/src/test/resources/test-mvc.xml
Normal file
24
spring-mvc-basics-4/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>
|
||||
Reference in New Issue
Block a user