From d8b0480a080d911af9a7973c63b498c4689236ab Mon Sep 17 00:00:00 2001 From: Adam Ruka Date: Thu, 29 Jan 2015 15:27:24 +0100 Subject: [PATCH] Example of a bug - adding a Spring Web MVC app to the Spring Boot example farm results in an error. --- .../spring-boot-app/build.gradle | 1 + .../spring-web-mvc-webservice/build.gradle | 7 +++++ .../gretty/springmvcapp/MyMvcController.java | 14 ++++++++++ .../springmvcapp/WebAppInitializer.java | 27 +++++++++++++++++++ .../gretty/springmvcapp/WebConfig.java | 12 +++++++++ 5 files changed, 61 insertions(+) create mode 100644 examples/spring-boot-farm/spring-web-mvc-webservice/build.gradle create mode 100644 examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/MyMvcController.java create mode 100644 examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebAppInitializer.java create mode 100644 examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebConfig.java diff --git a/examples/spring-boot-farm/spring-boot-app/build.gradle b/examples/spring-boot-farm/spring-boot-app/build.gradle index f2ed3fe2f..39d210a0c 100644 --- a/examples/spring-boot-farm/spring-boot-app/build.gradle +++ b/examples/spring-boot-farm/spring-boot-app/build.gradle @@ -16,6 +16,7 @@ farm { webapp project webapp ':spring-boot-farm:spring-boot-webservice' webapp ':spring-boot-farm:jee-webservice' + webapp ':spring-boot-farm:spring-web-mvc-webservice' } ext { diff --git a/examples/spring-boot-farm/spring-web-mvc-webservice/build.gradle b/examples/spring-boot-farm/spring-web-mvc-webservice/build.gradle new file mode 100644 index 000000000..752dbb302 --- /dev/null +++ b/examples/spring-boot-farm/spring-web-mvc-webservice/build.gradle @@ -0,0 +1,7 @@ +apply plugin: 'war' +apply plugin: 'org.akhikhl.gretty' +apply from: rootProject.file('integrationTests.gradle') + +dependencies { + compile "org.springframework:spring-webmvc:4.1.2.RELEASE" +} diff --git a/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/MyMvcController.java b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/MyMvcController.java new file mode 100644 index 000000000..0fe56a917 --- /dev/null +++ b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/MyMvcController.java @@ -0,0 +1,14 @@ +package org.akhikhl.examples.gretty.springmvcapp; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("test") +public class MyMvcController { + @RequestMapping(method = RequestMethod.GET) + public String test() { + return "MVC TEST"; + } +} diff --git a/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebAppInitializer.java b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebAppInitializer.java new file mode 100644 index 000000000..bc82cf1a5 --- /dev/null +++ b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebAppInitializer.java @@ -0,0 +1,27 @@ +package org.akhikhl.examples.gretty.springmvcapp; + +import javax.servlet.ServletContext; +import javax.servlet.ServletRegistration; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class WebAppInitializer implements WebApplicationInitializer { + @Override + public void onStartup(ServletContext container) { + WebApplicationContext appContext = getContext(); + + ServletRegistration.Dynamic dispatcher = container.addServlet( + "dispatcher", new DispatcherServlet(appContext)); + dispatcher.setLoadOnStartup(1); + dispatcher.addMapping("/"); + } + + private AnnotationConfigWebApplicationContext getContext() { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.setConfigLocation("org.akhikhl.examples.gretty.springmvcapp"); + return context; + } +} diff --git a/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebConfig.java b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebConfig.java new file mode 100644 index 000000000..12d085ebe --- /dev/null +++ b/examples/spring-boot-farm/spring-web-mvc-webservice/src/main/java/org/akhikhl/examples/gretty/springmvcapp/WebConfig.java @@ -0,0 +1,12 @@ +package org.akhikhl.examples.gretty.springmvcapp; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = "org.akhikhl.examples.gretty.springmvcapp") +public class WebConfig extends WebMvcConfigurerAdapter { +}