JAVA-14723 Convert spring-mvc-basics-4 to Spring Boot project and upd… (#13399)
* JAVA-14723 Convert spring-mvc-basics-4 to Spring Boot project and update articles * JAVA-14723 Refactoring and remove unused files * JAVA-14723 remove the WEB-INF and migrate jsp to html * JAVA-14723 Add back the removed test and rename the test into: ArticleViewerControllerWithRequiredAttributeIntegrationTest --------- Co-authored-by: timis1 <noreplay@yahoo.com>
This commit is contained in:
		
							parent
							
								
									a04d35a6d8
								
							
						
					
					
						commit
						95026e6b88
					
				| @ -15,26 +15,26 @@ | |||||||
|     </parent> |     </parent> | ||||||
| 
 | 
 | ||||||
|     <dependencies> |     <dependencies> | ||||||
|         <dependency> |  | ||||||
|             <groupId>com.fasterxml.jackson.core</groupId> |  | ||||||
|             <artifactId>jackson-databind</artifactId> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |  | ||||||
|             <groupId>org.springframework</groupId> |  | ||||||
|             <artifactId>spring-web</artifactId> |  | ||||||
|         </dependency> |  | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.springframework.boot</groupId> |             <groupId>org.springframework.boot</groupId> | ||||||
|             <artifactId>spring-boot-starter-validation</artifactId> |             <artifactId>spring-boot-starter-validation</artifactId> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>javax.servlet</groupId> |             <groupId>org.springframework.boot</groupId> | ||||||
|             <artifactId>javax.servlet-api</artifactId> |             <artifactId>spring-boot-starter-web</artifactId> | ||||||
|             <scope>provided</scope> |             <version>3.0.2</version> | ||||||
|         </dependency> |         </dependency> | ||||||
|         <dependency> |         <dependency> | ||||||
|             <groupId>org.springframework</groupId> |             <groupId>org.apache.tomcat.embed</groupId> | ||||||
|             <artifactId>spring-webmvc</artifactId> |             <artifactId>tomcat-embed-jasper</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>javax.servlet</groupId> | ||||||
|  |             <artifactId>jstl</artifactId> | ||||||
|  |         </dependency> | ||||||
|  |         <dependency> | ||||||
|  |             <groupId>org.springframework.boot</groupId> | ||||||
|  |             <artifactId>spring-boot-starter-thymeleaf</artifactId> | ||||||
|         </dependency> |         </dependency> | ||||||
|     </dependencies> |     </dependencies> | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -0,0 +1,87 @@ | |||||||
|  | package com.baeldung.config; | ||||||
|  | 
 | ||||||
|  | import org.springframework.boot.web.server.WebServerFactoryCustomizer; | ||||||
|  | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; | ||||||
|  | import org.springframework.context.annotation.Bean; | ||||||
|  | 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.view.InternalResourceViewResolver; | ||||||
|  | import org.springframework.web.servlet.view.JstlView; | ||||||
|  | import org.thymeleaf.spring5.SpringTemplateEngine; | ||||||
|  | import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; | ||||||
|  | import org.thymeleaf.spring5.view.ThymeleafViewResolver; | ||||||
|  | import org.thymeleaf.templatemode.TemplateMode; | ||||||
|  | import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; | ||||||
|  | import org.thymeleaf.templateresolver.ITemplateResolver; | ||||||
|  | 
 | ||||||
|  | import com.baeldung.contexts.Greeting; | ||||||
|  | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Web Configuration for the entire app | ||||||
|  |  */ | ||||||
|  | @Configuration | ||||||
|  | @EnableWebMvc | ||||||
|  | public class WebConfig { | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> enableDefaultServlet() { | ||||||
|  |         return factory -> factory.setRegisterDefaultServlet(true); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public Greeting greeting() { | ||||||
|  |         Greeting greeting = new Greeting(); | ||||||
|  |         greeting.setMessage("Hello World !!"); | ||||||
|  |         return greeting; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public ObjectMapper objectMapper() { | ||||||
|  |         return new ObjectMapper(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Thymeleaf configuration | ||||||
|  |     @Bean | ||||||
|  |     public ViewResolver thymeleafViewResolver() { | ||||||
|  | 
 | ||||||
|  |         ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); | ||||||
|  | 
 | ||||||
|  |         viewResolver.setTemplateEngine(thymeleafTemplateEngine()); | ||||||
|  |         viewResolver.setCharacterEncoding("UTF-8"); | ||||||
|  |         viewResolver.setOrder(0); | ||||||
|  | 
 | ||||||
|  |         return viewResolver; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // Thymeleaf template engine with Spring integration | ||||||
|  |     @Bean | ||||||
|  |     public SpringTemplateEngine thymeleafTemplateEngine() { | ||||||
|  | 
 | ||||||
|  |         SpringTemplateEngine templateEngine = new SpringTemplateEngine(); | ||||||
|  |         templateEngine.setTemplateResolver(thymeleafTemplateResolver()); | ||||||
|  |         templateEngine.setEnableSpringELCompiler(true); | ||||||
|  | 
 | ||||||
|  |         return templateEngine; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public SpringResourceTemplateResolver springResourceTemplateResolver() { | ||||||
|  |         return new SpringResourceTemplateResolver(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Bean | ||||||
|  |     public ITemplateResolver thymeleafTemplateResolver() { | ||||||
|  | 
 | ||||||
|  |         ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); | ||||||
|  | 
 | ||||||
|  |         templateResolver.setPrefix("/templates/"); | ||||||
|  |         templateResolver.setCacheable(false); | ||||||
|  |         templateResolver.setSuffix(".html"); | ||||||
|  |         templateResolver.setTemplateMode(TemplateMode.HTML); | ||||||
|  |         templateResolver.setCharacterEncoding("UTF-8"); | ||||||
|  | 
 | ||||||
|  |         return templateResolver; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -1,37 +0,0 @@ | |||||||
| 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"; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,16 +0,0 @@ | |||||||
| 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; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,41 +0,0 @@ | |||||||
| 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/*"); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,25 +0,0 @@ | |||||||
| 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; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,19 +0,0 @@ | |||||||
| 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; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,33 +0,0 @@ | |||||||
| 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"; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,25 +0,0 @@ | |||||||
| 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; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -3,9 +3,9 @@ package com.baeldung.contexts.normal; | |||||||
| import java.util.Arrays; | import java.util.Arrays; | ||||||
| 
 | 
 | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
|  | import org.springframework.context.ApplicationContext; | ||||||
| import org.springframework.stereotype.Controller; | import org.springframework.stereotype.Controller; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.context.ContextLoader; |  | ||||||
| import org.springframework.web.context.WebApplicationContext; | import org.springframework.web.context.WebApplicationContext; | ||||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||||
| 
 | 
 | ||||||
| @ -19,21 +19,22 @@ public class HelloWorldController { | |||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|     private GreeterService greeterService; |     private GreeterService greeterService; | ||||||
|  | 
 | ||||||
|  |     @Autowired | ||||||
|  |     private ApplicationContext applicationContext; | ||||||
|      |      | ||||||
|     private void processContext() { |     private void processContext() { | ||||||
|         WebApplicationContext rootContext = ContextLoader.getCurrentWebApplicationContext(); |         System.out.println("root context : " + applicationContext); | ||||||
|          |         System.out.println("root context Beans: " + Arrays.asList(applicationContext.getBeanDefinitionNames())); | ||||||
|         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 : " + webApplicationContext); | ||||||
|         System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); |         System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @RequestMapping(path = "/welcome") |     @GetMapping(path = "/welcome") | ||||||
|     public ModelAndView helloWorld() { |     public ModelAndView helloWorld() { | ||||||
|         processContext(); |         processContext(); | ||||||
|         String message = "<br><div style='text-align:center;'>" + "<h3>Normal " + greeterService.greet() + "</h3></div>"; |         String message = "<br><div style='text-align:center;'>" + "<h3>Normal " + greeterService.greet() + "</h3></div>"; | ||||||
|         return new ModelAndView("welcome", "message", message); |         return new ModelAndView("/view/welcome", "message", message); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -6,8 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; | |||||||
| import org.springframework.beans.factory.annotation.Qualifier; | import org.springframework.beans.factory.annotation.Qualifier; | ||||||
| import org.springframework.context.ApplicationContext; | import org.springframework.context.ApplicationContext; | ||||||
| import org.springframework.stereotype.Controller; | import org.springframework.stereotype.Controller; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.context.ContextLoader; |  | ||||||
| import org.springframework.web.context.WebApplicationContext; | import org.springframework.web.context.WebApplicationContext; | ||||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||||
| 
 | 
 | ||||||
| @ -31,19 +30,15 @@ public class HelloWorldSecureController { | |||||||
|         ApplicationContext context = contextUtilService.getApplicationContext(); |         ApplicationContext context = contextUtilService.getApplicationContext(); | ||||||
|         System.out.println("application context : " + context); |         System.out.println("application context : " + context); | ||||||
|         System.out.println("application context Beans: " + Arrays.asList(context.getBeanDefinitionNames())); |         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 : " + webApplicationContext); | ||||||
|         System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); |         System.out.println("context Beans: " + Arrays.asList(webApplicationContext.getBeanDefinitionNames())); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @RequestMapping(path = "/welcome") |     @GetMapping(path = "/welcome_secure") | ||||||
|     public ModelAndView helloWorld() { |     public ModelAndView helloWorld() { | ||||||
|         processContext(); |         processContext(); | ||||||
|         String message = "<br><div style='text-align:center;'>" + "<h3>Secure " + greeterService.greet() + "</h3></div>"; |         String message = "<br><div style='text-align:center;'>" + "<h3>Secure " + greeterService.greet() + "</h3></div>"; | ||||||
|         return new ModelAndView("welcome", "message", message); |         return new ModelAndView("/secure/view/welcome", "message", message); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,28 +0,0 @@ | |||||||
| package com.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/*"); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -1,28 +0,0 @@ | |||||||
| package com.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 = { "com.baeldung.controller", "com.baeldung.optionalpathvars" }) |  | ||||||
| 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; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @ -7,42 +7,26 @@ import org.springframework.http.HttpHeaders; | |||||||
| import org.springframework.http.HttpStatus; | import org.springframework.http.HttpStatus; | ||||||
| import org.springframework.http.MediaType; | import org.springframework.http.MediaType; | ||||||
| import org.springframework.http.ResponseEntity; | import org.springframework.http.ResponseEntity; | ||||||
| import org.springframework.stereotype.Controller; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RestController; | ||||||
| import org.springframework.web.bind.annotation.RequestMethod; |  | ||||||
| import org.springframework.web.bind.annotation.ResponseBody; |  | ||||||
| 
 | 
 | ||||||
| @Controller | @RestController | ||||||
| public class GreetingsController { | public class GreetingsController { | ||||||
|      |      | ||||||
|     @RequestMapping( |     @GetMapping(value = "/greetings-with-response-body", produces="application/json") | ||||||
|         value = "/greetings-with-response-body",  |  | ||||||
|         method = RequestMethod.GET,  |  | ||||||
|         produces="application/json" |  | ||||||
|     )  |  | ||||||
|     @ResponseBody |  | ||||||
|     public String getGreetingWhileReturnTypeIsString() {  |     public String getGreetingWhileReturnTypeIsString() {  | ||||||
|         return "{\"test\": \"Hello using @ResponseBody\"}"; |         return "{\"test\": \"Hello\"}"; | ||||||
|     } |     } | ||||||
|          |          | ||||||
|     @RequestMapping( |     @GetMapping(value = "/greetings-with-response-entity", produces = "application/json") | ||||||
|         value = "/greetings-with-response-entity", |  | ||||||
|         method = RequestMethod.GET,  |  | ||||||
|         produces = "application/json" |  | ||||||
|     ) |  | ||||||
|     public ResponseEntity<String> getGreetingWithResponseEntity() { |     public ResponseEntity<String> getGreetingWithResponseEntity() { | ||||||
|         final HttpHeaders httpHeaders= new HttpHeaders(); |         final HttpHeaders httpHeaders= new HttpHeaders(); | ||||||
|         httpHeaders.setContentType(MediaType.APPLICATION_JSON); |         httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||||||
|         return new ResponseEntity<String>("{\"test\": \"Hello with ResponseEntity\"}", httpHeaders, HttpStatus.OK); |         return new ResponseEntity<>("{\"test\": \"Hello with ResponseEntity\"}", httpHeaders, HttpStatus.OK); | ||||||
|     } |     } | ||||||
|     @RequestMapping( |     @GetMapping(value = "/greetings-with-map-return-type", produces = "application/json") | ||||||
|         value = "/greetings-with-map-return-type",  |  | ||||||
|         method = RequestMethod.GET,  |  | ||||||
|         produces = "application/json" |  | ||||||
|     ) |  | ||||||
|     @ResponseBody |  | ||||||
|     public Map<String, Object> getGreetingWhileReturnTypeIsMap() { |     public Map<String, Object> getGreetingWhileReturnTypeIsMap() { | ||||||
|         HashMap<String, Object> map = new HashMap<String, Object>(); |         HashMap<String, Object> map = new HashMap<>(); | ||||||
|         map.put("test", "Hello from map"); |         map.put("test", "Hello from map"); | ||||||
|         return map; |         return map; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -18,19 +18,19 @@ public class PassParametersController { | |||||||
|     @GetMapping("/showViewPage") |     @GetMapping("/showViewPage") | ||||||
|     public String passParametersWithModel(Model model) { |     public String passParametersWithModel(Model model) { | ||||||
|         model.addAttribute("message", "Baeldung"); |         model.addAttribute("message", "Baeldung"); | ||||||
|         return "viewPage"; |         return "view/viewPage"; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/printViewPage") |     @GetMapping("/printViewPage") | ||||||
|     public String passParametersWithModelMap(ModelMap map) { |     public String passParametersWithModelMap(ModelMap map) { | ||||||
|         map.addAttribute("welcomeMessage", "welcome"); |         map.addAttribute("welcomeMessage", "welcome"); | ||||||
|         map.addAttribute("message", "Baeldung"); |         map.addAttribute("message", "Baeldung"); | ||||||
|         return "viewPage"; |         return "view/viewPage"; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/goToViewPage") |     @GetMapping("/goToViewPage") | ||||||
|     public ModelAndView passParametersWithModelAndView() { |     public ModelAndView passParametersWithModelAndView() { | ||||||
|         ModelAndView modelAndView = new ModelAndView("viewPage"); |         ModelAndView modelAndView = new ModelAndView("view/viewPage"); | ||||||
|         modelAndView.addObject("message", "Baeldung"); |         modelAndView.addObject("message", "Baeldung"); | ||||||
|         return modelAndView; |         return modelAndView; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -1,17 +1,15 @@ | |||||||
| package com.baeldung.controller.controller; | package com.baeldung.controller.controller; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.controller.student.Student; |  | ||||||
| import org.springframework.stereotype.Controller; |  | ||||||
| import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.bind.annotation.PathVariable; | import org.springframework.web.bind.annotation.PathVariable; | ||||||
| import org.springframework.web.bind.annotation.ResponseBody; |  | ||||||
| 
 | 
 | ||||||
| @Controller | import com.baeldung.controller.student.Student; | ||||||
|  | 
 | ||||||
|  | @org.springframework.web.bind.annotation.RestController | ||||||
| public class RestController { | public class RestController { | ||||||
| 
 | 
 | ||||||
|     @GetMapping(value = "/student/{studentId}") |     @GetMapping(value = "/student/{studentId}") | ||||||
|     public @ResponseBody |     public Student getTestData(@PathVariable Integer studentId) { | ||||||
|     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); | ||||||
|  | |||||||
| @ -27,7 +27,14 @@ public class Student { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public boolean equals(Object obj) { |     public boolean equals(Object o) { | ||||||
|         return this.name.equals(((Student) obj).getName()); |         if (this == o) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |         if (!(o instanceof Student)) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |         Student student = (Student) o; | ||||||
|  |         return getName().equals(student.getName()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -1,36 +0,0 @@ | |||||||
| package com.baeldung.jsonparams.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; |  | ||||||
| 
 |  | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; |  | ||||||
| 
 |  | ||||||
| @Configuration |  | ||||||
| @EnableWebMvc |  | ||||||
| @ComponentScan(basePackages = { "com.baeldung.jsonparams" }) |  | ||||||
| public class JsonParamsConfig 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; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     @Bean |  | ||||||
|     public ObjectMapper objectMapper() { |  | ||||||
|         return new ObjectMapper(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,29 +0,0 @@ | |||||||
| package com.baeldung.jsonparams.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 JsonParamsInit // implements WebApplicationInitializer  |  | ||||||
| { |  | ||||||
| 
 |  | ||||||
|     //uncomment to run the product controller example |  | ||||||
|     //@Override |  | ||||||
|     public void onStartup(ServletContext sc) throws ServletException { |  | ||||||
|         AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); |  | ||||||
|         root.register(JsonParamsConfig.class); |  | ||||||
|         root.setServletContext(sc); |  | ||||||
|         sc.addListener(new ContextLoaderListener(root)); |  | ||||||
| 
 |  | ||||||
|         DispatcherServlet dv = new DispatcherServlet(root); |  | ||||||
| 
 |  | ||||||
|         ServletRegistration.Dynamic appServlet = sc.addServlet("jsonparams-mvc", dv); |  | ||||||
|         appServlet.setLoadOnStartup(1); |  | ||||||
|         appServlet.addMapping("/"); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -1,7 +1,6 @@ | |||||||
| package com.baeldung.jsonparams.controller; | package com.baeldung.jsonparams.controller; | ||||||
| 
 | 
 | ||||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||||
| import org.springframework.stereotype.Controller; |  | ||||||
| import org.springframework.web.bind.WebDataBinder; | import org.springframework.web.bind.WebDataBinder; | ||||||
| import org.springframework.web.bind.annotation.GetMapping; | import org.springframework.web.bind.annotation.GetMapping; | ||||||
| import org.springframework.web.bind.annotation.InitBinder; | import org.springframework.web.bind.annotation.InitBinder; | ||||||
| @ -9,7 +8,7 @@ import org.springframework.web.bind.annotation.PostMapping; | |||||||
| import org.springframework.web.bind.annotation.RequestBody; | import org.springframework.web.bind.annotation.RequestBody; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RequestParam; | import org.springframework.web.bind.annotation.RequestParam; | ||||||
| import org.springframework.web.bind.annotation.ResponseBody; | import org.springframework.web.bind.annotation.RestController; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.jsonparams.model.Product; | import com.baeldung.jsonparams.model.Product; | ||||||
| import com.baeldung.jsonparams.propertyeditor.ProductEditor; | import com.baeldung.jsonparams.propertyeditor.ProductEditor; | ||||||
| @ -17,7 +16,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; | |||||||
| import com.fasterxml.jackson.databind.JsonMappingException; | import com.fasterxml.jackson.databind.JsonMappingException; | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| 
 | 
 | ||||||
| @Controller | @RestController | ||||||
| @RequestMapping("/products") | @RequestMapping("/products") | ||||||
| public class ProductController { | public class ProductController { | ||||||
| 
 | 
 | ||||||
| @ -34,21 +33,18 @@ public class ProductController { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @PostMapping("/create") |     @PostMapping("/create") | ||||||
|     @ResponseBody |  | ||||||
|     public Product createProduct(@RequestBody Product product) { |     public Product createProduct(@RequestBody Product product) { | ||||||
|         // custom logic |         // custom logic | ||||||
|         return product; |         return product; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/get") |     @GetMapping("/get") | ||||||
|     @ResponseBody |     public Product getProduct(@RequestParam String product) throws JsonProcessingException { | ||||||
|     public Product getProduct(@RequestParam String product) throws JsonMappingException, JsonProcessingException { |  | ||||||
|         final Product prod = objectMapper.readValue(product, Product.class); |         final Product prod = objectMapper.readValue(product, Product.class); | ||||||
|         return prod; |         return prod; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @GetMapping("/get2") |     @GetMapping("/get2") | ||||||
|     @ResponseBody |  | ||||||
|     public Product get2Product(@RequestParam Product product) { |     public Product get2Product(@RequestParam Product product) { | ||||||
|         // custom logic |         // custom logic | ||||||
|         return product; |         return product; | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ public class ProductEditor extends PropertyEditorSupport { | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void setAsText(String text) throws IllegalArgumentException { |     public void setAsText(String text) throws IllegalArgumentException { | ||||||
|         if (StringUtils.isEmpty(text)) { |         if (!StringUtils.hasText(text)) { | ||||||
|             setValue(null); |             setValue(null); | ||||||
|         } else { |         } else { | ||||||
|             Product prod = new Product(); |             Product prod = new Product(); | ||||||
|  | |||||||
| @ -1,23 +0,0 @@ | |||||||
| package com.baeldung.optionalpathvars; |  | ||||||
| 
 |  | ||||||
| import static com.baeldung.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; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| @ -4,7 +4,7 @@ import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; | |||||||
| 
 | 
 | ||||||
| import org.springframework.web.bind.annotation.PathVariable; | import org.springframework.web.bind.annotation.PathVariable; | ||||||
| import org.springframework.web.bind.annotation.RequestMapping; | import org.springframework.web.bind.annotation.RequestMapping; | ||||||
| import org.springframework.web.bind.annotation.RestController;; | import org.springframework.web.bind.annotation.RestController; | ||||||
| 
 | 
 | ||||||
| @RestController | @RestController | ||||||
| @RequestMapping(value = "/requiredAttribute") | @RequestMapping(value = "/requiredAttribute") | ||||||
|  | |||||||
| @ -5,7 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||||
| import org.springframework.context.annotation.ComponentScan; | import org.springframework.context.annotation.ComponentScan; | ||||||
| import org.springframework.context.annotation.Configuration; | import org.springframework.context.annotation.Configuration; | ||||||
| 
 | 
 | ||||||
| @ComponentScan(basePackages = "com.baeldung.validation.listvalidation") | @ComponentScan(basePackages = "com.baeldung") | ||||||
| @Configuration | @Configuration | ||||||
| @SpringBootApplication | @SpringBootApplication | ||||||
| public class SpringListValidationApplication { | public class SpringListValidationApplication { | ||||||
|  | |||||||
| @ -0,0 +1,11 @@ | |||||||
|  | <!DOCTYPE HTML> | ||||||
|  | <html xmlns:th="http://www.thymeleaf.org"> | ||||||
|  | <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 : <span th:utext="${message}"></span> | ||||||
|  | 	</div> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
| @ -4,6 +4,6 @@ | |||||||
|     <title>Title</title> |     <title>Title</title> | ||||||
| </head> | </head> | ||||||
| <body> | <body> | ||||||
|     <div>Web Application. Passed parameter : th:text="${message}"</div> |     <div>Web Application. Passed parameter : <span th:text="${message}"></span></div> | ||||||
| </body> | </body> | ||||||
| </html> | </html> | ||||||
| @ -1,11 +1,12 @@ | |||||||
| <html> | <!DOCTYPE HTML> | ||||||
|  | <html xmlns:th="http://www.thymeleaf.org"> | ||||||
| <head> | <head> | ||||||
|     <title>Spring Web Contexts</title> |     <title>Spring Web Contexts</title> | ||||||
| </head> | </head> | ||||||
| <body> | <body> | ||||||
|     <br> |     <br> | ||||||
|     <div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;"> |     <div style="padding: 10px; border-radius: 10px; font-size: 30px; text-align: center;"> | ||||||
|         Normal Web Application : ${message} |         Normal Web Application : <span th:utext="${message}"></span> | ||||||
|     </div> |     </div> | ||||||
| </body> | </body> | ||||||
| </html> | </html> | ||||||
| @ -0,0 +1,10 @@ | |||||||
|  | <!DOCTYPE HTML> | ||||||
|  | <html xmlns:th="http://www.thymeleaf.org"> | ||||||
|  | <head> | ||||||
|  |     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||||||
|  |     <title>Insert title here</title> | ||||||
|  | </head> | ||||||
|  | <body> | ||||||
|  | Data returned is <span th:utext="${data}"></span> | ||||||
|  | </body> | ||||||
|  | </html> | ||||||
| @ -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.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> |  | ||||||
| @ -1,11 +0,0 @@ | |||||||
| <?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> |  | ||||||
| @ -1,5 +0,0 @@ | |||||||
| <html> |  | ||||||
| <body> |  | ||||||
| <h2>Hello World!</h2> |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
| @ -1,16 +0,0 @@ | |||||||
| <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> |  | ||||||
| @ -1,14 +0,0 @@ | |||||||
| <?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> |  | ||||||
| @ -1,16 +0,0 @@ | |||||||
| <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> |  | ||||||
| @ -1,11 +0,0 @@ | |||||||
| <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> |  | ||||||
| @ -1,7 +0,0 @@ | |||||||
| <html> |  | ||||||
| <head></head> |  | ||||||
| 
 |  | ||||||
| <body> |  | ||||||
| <h1>This is the body of the sample view</h1> |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
| @ -1,10 +0,0 @@ | |||||||
| <html> |  | ||||||
| <head></head> |  | ||||||
| 
 |  | ||||||
| <body> |  | ||||||
|     <h1>Bean Scopes Examples</h1> |  | ||||||
|     <br> Previous Message: ${previousMessage } |  | ||||||
|     <br> Current Message: ${currentMessage } |  | ||||||
|     <br> |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
| @ -1,88 +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"> |  | ||||||
| 
 |  | ||||||
|     <!-- 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>com.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> |  | ||||||
| @ -1,12 +0,0 @@ | |||||||
| <%@ 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> |  | ||||||
| @ -1,5 +0,0 @@ | |||||||
| <html> |  | ||||||
| <body> |  | ||||||
| <h2>Hello World!</h2> |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
| @ -1,26 +1,24 @@ | |||||||
| package com.baeldung.controller; | package com.baeldung.controller; | ||||||
| 
 | 
 | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; |  | ||||||
| import com.baeldung.controller.config.WebConfig; |  | ||||||
| import com.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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.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) | import com.baeldung.controller.student.Student; | ||||||
| @WebAppConfiguration | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| @ContextConfiguration(classes = { WebConfig.class }, loader = AnnotationConfigWebContextLoader.class) | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
|  | 
 | ||||||
|  | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| public class ControllerAnnotationIntegrationTest { | public class ControllerAnnotationIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
|  | |||||||
| @ -5,9 +5,8 @@ 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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; | ||||||
| @ -15,11 +14,11 @@ import org.springframework.web.context.WebApplicationContext; | |||||||
| import org.springframework.web.servlet.ModelAndView; | import org.springframework.web.servlet.ModelAndView; | ||||||
| 
 | 
 | ||||||
| import com.baeldung.controller.student.Student; | import com.baeldung.controller.student.Student; | ||||||
|  | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | @RunWith(SpringRunner.class) | ||||||
| @WebAppConfiguration | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| @ContextConfiguration({ "classpath:test-mvc.xml" }) |  | ||||||
| public class ControllerIntegrationTest { | public class ControllerIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
|  | |||||||
| @ -1,24 +1,21 @@ | |||||||
| package com.baeldung.controller; | package com.baeldung.controller; | ||||||
| 
 | 
 | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; |  | ||||||
| import com.baeldung.controller.controller.GreetingsController; |  | ||||||
| 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.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; | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| @WebAppConfiguration | 
 | ||||||
| @ContextConfiguration(classes = { GreetingsController.class }, loader = AnnotationConfigWebContextLoader.class) | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| public class GreetingsControllerUnitTest { | public class GreetingsControllerUnitTest { | ||||||
|      |      | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
|  | |||||||
| @ -5,24 +5,24 @@ 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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; | ||||||
| 
 | 
 | ||||||
|  | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * This is the test class for {@link com.baeldung.controller.controller.PassParametersController} class. |  * This is the test class for {@link com.baeldung.controller.controller.PassParametersController} class. | ||||||
|  * 09/09/2017 |  * 09/09/2017 | ||||||
|  * |  * | ||||||
|  * @author Ahmet Cetin |  * @author Ahmet Cetin | ||||||
|  */ |  */ | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | @RunWith(SpringRunner.class) | ||||||
| @WebAppConfiguration | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| @ContextConfiguration({"classpath:test-mvc.xml"}) |  | ||||||
| public class PassParametersControllerIntegrationTest { | public class PassParametersControllerIntegrationTest { | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,26 +1,25 @@ | |||||||
| package com.baeldung.jsonparams; | package com.baeldung.jsonparams; | ||||||
| 
 | 
 | ||||||
|  | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||||||
|  | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||||||
|  | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||||||
|  | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||||||
|  | 
 | ||||||
| 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.http.MediaType; | import org.springframework.http.MediaType; | ||||||
| import org.springframework.test.context.ContextConfiguration; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.MockMvc; | ||||||
| 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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |  | ||||||
| 
 | 
 | ||||||
| import com.baeldung.jsonparams.config.JsonParamsConfig; | @RunWith(SpringRunner.class) | ||||||
| 
 | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) |  | ||||||
| @WebAppConfiguration |  | ||||||
| @ContextConfiguration(classes = { JsonParamsConfig.class }, loader = AnnotationConfigWebContextLoader.class) |  | ||||||
| public class JsonParamsIntegrationTest { | public class JsonParamsIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
|  | |||||||
| @ -1,54 +0,0 @@ | |||||||
| package com.baeldung.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 com.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()); |  | ||||||
|                 |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|      |  | ||||||
| } |  | ||||||
| @ -4,19 +4,18 @@ 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.result.MockMvcResultMatchers; | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||||||
| 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 com.baeldung.controller.config.WebConfig; |  | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| @WebAppConfiguration | 
 | ||||||
| @ContextConfiguration(classes = { WebConfig.class }) | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| public class ArticleViewerControllerWithOptionalParamIntegrationTest { | public class ArticleViewerControllerWithOptionalParamIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|  | |||||||
| @ -12,42 +12,39 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | |||||||
| import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||||||
| 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 com.baeldung.controller.config.WebConfig; | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | @RunWith(SpringJUnit4ClassRunner.class) | ||||||
| @WebAppConfiguration | @WebAppConfiguration | ||||||
| @ContextConfiguration(classes = { WebConfig.class }) | @ContextConfiguration(classes = { SpringListValidationApplication.class }) | ||||||
| public class ArticleViewerControllerWithRequiredAttributeIntegrationTest { | public class ArticleViewerControllerWithRequiredAttributeIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|     private WebApplicationContext wac; |     private WebApplicationContext wac; | ||||||
|   | 
 | ||||||
|     private MockMvc mockMvc; |     private MockMvc mockMvc; | ||||||
| 
 | 
 | ||||||
|     @Before |     @Before | ||||||
|     public void setup() throws Exception { |     public void setup() { | ||||||
|         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); |         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenRequiredAttributeIsFalse_whenIdPathVariableIsPassed_thenResponseOK() throws Exception { |     public void whenIdPathVariableIsPassed_thenResponseOK() throws Exception { | ||||||
|      |         int articleId = 5; | ||||||
|         int articleId = 154; |  | ||||||
|          |          | ||||||
|         this.mockMvc |         this.mockMvc | ||||||
|             .perform(MockMvcRequestBuilders.get("/requiredAttribute/article/{id}", articleId)) |             .perform(MockMvcRequestBuilders.get("/article/{id}", articleId)) | ||||||
|             .andExpect(MockMvcResultMatchers.status().isOk()) |             .andExpect(MockMvcResultMatchers.status().isOk()) | ||||||
|             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId)); |             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(articleId)); | ||||||
|                  |                 | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenRequiredAttributeIsFalse_whenIdPathVariableIsNotPassed_thenResponseOK() throws Exception { |     public void whenIdPathVariableIsNotPassed_thenResponse500() throws Exception { | ||||||
|              |  | ||||||
|         this.mockMvc |         this.mockMvc | ||||||
|             .perform(MockMvcRequestBuilders.get("/requiredAttribute/article")) |             .perform(MockMvcRequestBuilders.get("/article")) | ||||||
|             .andExpect(MockMvcResultMatchers.status().isOk()) |             .andExpect(MockMvcResultMatchers.status().isInternalServerError()); | ||||||
|             .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(Article.DEFAULT_ARTICLE.getId())); |                 | ||||||
|                  |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -4,19 +4,18 @@ 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.result.MockMvcResultMatchers; | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||||||
| 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 com.baeldung.controller.config.WebConfig; |  | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| @WebAppConfiguration | 
 | ||||||
| @ContextConfiguration(classes = { WebConfig.class }) | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| public class ArticleViewerWithMapParamIntegrationTest { | public class ArticleViewerWithMapParamIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|  | |||||||
| @ -4,19 +4,18 @@ 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.boot.test.context.SpringBootTest; | ||||||
| import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| 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.result.MockMvcResultMatchers; | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||||||
| 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 com.baeldung.controller.config.WebConfig; |  | ||||||
| 
 | 
 | ||||||
| @RunWith(SpringJUnit4ClassRunner.class) | import com.baeldung.validation.listvalidation.SpringListValidationApplication; | ||||||
| @WebAppConfiguration | 
 | ||||||
| @ContextConfiguration(classes = { WebConfig.class }) | @RunWith(SpringRunner.class) | ||||||
|  | @SpringBootTest(classes = SpringListValidationApplication.class) | ||||||
| public class ArticleViewerWithTwoSeparateMethodsIntegrationTest { | public class ArticleViewerWithTwoSeparateMethodsIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     @Autowired |     @Autowired | ||||||
|  | |||||||
| @ -34,7 +34,7 @@ public class MovieControllerIntegrationTest { | |||||||
|         Movie movie = new Movie("Movie3"); |         Movie movie = new Movie("Movie3"); | ||||||
|         movies.add(movie); |         movies.add(movie); | ||||||
|         mvc.perform(MockMvcRequestBuilders.post("/movies") |         mvc.perform(MockMvcRequestBuilders.post("/movies") | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |             .contentType(MediaType.APPLICATION_JSON) | ||||||
|             .content(objectMapper.writeValueAsString(movies))) |             .content(objectMapper.writeValueAsString(movies))) | ||||||
|             .andExpect(MockMvcResultMatchers.status() |             .andExpect(MockMvcResultMatchers.status() | ||||||
|                 .isOk()); |                 .isOk()); | ||||||
| @ -44,7 +44,7 @@ public class MovieControllerIntegrationTest { | |||||||
|     public void givenEmptyMovieList_whenAddingMovieList_thenThrowBadRequest() throws Exception { |     public void givenEmptyMovieList_whenAddingMovieList_thenThrowBadRequest() throws Exception { | ||||||
|         List<Movie> movies = new ArrayList<>(); |         List<Movie> movies = new ArrayList<>(); | ||||||
|         mvc.perform(MockMvcRequestBuilders.post("/movies") |         mvc.perform(MockMvcRequestBuilders.post("/movies") | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |             .contentType(MediaType.APPLICATION_JSON) | ||||||
|             .content(objectMapper.writeValueAsString(movies))) |             .content(objectMapper.writeValueAsString(movies))) | ||||||
|             .andExpect(MockMvcResultMatchers.status() |             .andExpect(MockMvcResultMatchers.status() | ||||||
|                 .isBadRequest()); |                 .isBadRequest()); | ||||||
| @ -54,7 +54,7 @@ public class MovieControllerIntegrationTest { | |||||||
|     public void givenEmptyMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception { |     public void givenEmptyMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception { | ||||||
|         Movie movie = new Movie(""); |         Movie movie = new Movie(""); | ||||||
|         mvc.perform(MockMvcRequestBuilders.post("/movies") |         mvc.perform(MockMvcRequestBuilders.post("/movies") | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |             .contentType(MediaType.APPLICATION_JSON) | ||||||
|             .content(objectMapper.writeValueAsString(Arrays.asList(movie)))) |             .content(objectMapper.writeValueAsString(Arrays.asList(movie)))) | ||||||
|             .andExpect(MockMvcResultMatchers.status() |             .andExpect(MockMvcResultMatchers.status() | ||||||
|                 .isBadRequest()); |                 .isBadRequest()); | ||||||
| @ -74,7 +74,7 @@ public class MovieControllerIntegrationTest { | |||||||
|         movies.add(movie4); |         movies.add(movie4); | ||||||
|         movies.add(movie5); |         movies.add(movie5); | ||||||
|         mvc.perform(MockMvcRequestBuilders.post("/movies") |         mvc.perform(MockMvcRequestBuilders.post("/movies") | ||||||
|             .contentType(MediaType.APPLICATION_JSON_UTF8) |             .contentType(MediaType.APPLICATION_JSON) | ||||||
|             .content(objectMapper.writeValueAsString(movies))) |             .content(objectMapper.writeValueAsString(movies))) | ||||||
|             .andExpect(MockMvcResultMatchers.status() |             .andExpect(MockMvcResultMatchers.status() | ||||||
|                 .isBadRequest()); |                 .isBadRequest()); | ||||||
|  | |||||||
| @ -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.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