如何使用 spring-boot 创建 REST 服务? [英] How to create a REST service with spring-boot?
本文介绍了如何使用 spring-boot 创建 REST 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 spring-boot
并且想要集成一个简单的 REST
服务,如下所示.
@Controller@RequestMapping("/内容")公共类 MyServiceRest 扩展 SpringBeanAutowiringSupport {@RequestMapping(method = RequestMethod.GET)公共字符串测试(){返回确定";}}
结果: localhost:8080/
结果 未找到服务."
.为什么?
我是否必须以某种方式显式发布服务?
也许是因为我的调度程序 servlet?
@Bean公共 ServletRegistrationBean dispatcherServletRegistration() {ServletRegistrationBean 注册 = new ServletRegistrationBean(new CXFServlet(), "/services/*");注册.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);退货登记;}
解决方案
从当前 spring-boot.1.5.6
开始,没有使用 cxf
的要求.>
只需将 @RestController
与 @GetMapping
一起使用,即可访问 localhost:8080/content
.
I'm using spring-boot
and want to integrate a simple REST
service as follows.
@Controller
@RequestMapping("/content")
public class MyServiceRest extends SpringBeanAutowiringSupport {
@RequestMapping(method = RequestMethod.GET)
public String test() {
return "OK";
}
}
Result: both localhost:8080/<app-name>/services/content
results "No service was found."
. Why?
Do I have to explicit publish the service somehow?
Maybe it is due to my dispatcher servlet?
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new CXFServlet(), "/services/*");
registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
return registration;
}
解决方案
As of current spring-boot.1.5.6
there is no requirement using cxf
.
Just use a @RestController
with @GetMapping
, and be happy to access localhost:8080/content
.
这篇关于如何使用 spring-boot 创建 REST 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文