3
嗨,我想通過Spring MVC的處理web請求,並在同一個項目球衣 處理其餘部分(彈簧引導)如何「由Spring MVC的Web請求」使用,「球衣休息」春天開機
當我測試Rest服務正在工作,但網絡不是 我如何設置應用程序配置?
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = {ProductsResource.class, MessageService.class,Web.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public ServletRegistrationBean jerseyServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/rest/*");
registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
return registration;
}
@Bean
public ServletRegistrationBean webMVC() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(ResourceConfig.class);
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "*.html");
servletRegistrationBean.setName("web-mvc");
return servletRegistrationBean;
}
網絡控制器
@Controller
@Component
public class Web {
@RequestMapping("/foo")
String foo() {
return "foo";
}
@RequestMapping("/bar")
String bar() {
return "bar";
}
}
休息控制器
@Path("/")
@Component
public class ProductsResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/hello")
public String hello() {
return "Hello World";
}
}