【发布时间】:2015-03-17 08:30:45
【问题描述】:
我正在使用spring-boot,想集成一个简单的REST服务如下。
@Controller
@RequestMapping("/content")
public class MyServiceRest extends SpringBeanAutowiringSupport {
@RequestMapping(method = RequestMethod.GET)
public String test() {
return "OK";
}
}
结果:localhost:8080/<app-name>/services/content 结果均为 "No service was found."。为什么?
我必须以某种方式显式发布服务吗?
也许是因为我的调度程序 servlet?
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new CXFServlet(), "/services/*");
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
【问题讨论】:
-
一般约定是localhost:8080/ApplicationName/content,试试看?
-
不幸的是同样的结果......
-
您的错误是否从 404 更改为“未找到服务”消息?
-
是的,我注意到我配置了调度程序 servlet,因此将路径更改为
...<app-name>/services/content将为我提供正确的路径。无论如何找不到该服务... -
@Controller是一个通用的 Spring MVC 注释,你需要用@EnableWebMvc注释你的配置类,无论使用 Spring Boot 还是 Tomcat,如果你想让 Spring 加载它们。如果您希望 Spring Boot 自动配置您的 REST 控制器,则将您的@Controller注释更改为@RestController并在您的配置类中使用@EnableAutoConfiguration。
标签: java spring rest spring-boot