【问题标题】:How to create a REST service with spring-boot?如何使用 spring-boot 创建 REST 服务?
【发布时间】: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


【解决方案1】:

截至目前的spring-boot.1.5.6,没有要求使用cxf

只需将@RestController@GetMapping 一起使用,即可访问localhost:8080/content

【讨论】:

    【解决方案2】:
    【解决方案3】:

    在我当前使用的最新版本的 Spring Boot 中,Web Service 的地址是http://localhost:8080/content

    另外,我用来启动服务的类如下所示:

    @ComponentScan("eu.buzea")
    @EnableAutoConfiguration
    @EnableTransactionManagement
    @SpringBootApplication
    public class Application{
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    【讨论】:

      【解决方案4】:

      您还应该为您的方法添加 url 映射

      @RequestMapping(method = RequestMethod.GET, value = "url_here", 试试

      @RequestMapping(method = RequestMethod.POST, value = "/",
      

      【讨论】:

      • 我尝试在方法级别显式添加映射,但也不起作用。
      • 您的 web.xml 中调度程序 servlet 的映射是什么?那也应该在你的路径中。
      • 也许调度员出了什么问题。我更新了上面的代码。
      【解决方案5】:

      由于您使用的是 Spring Boot,因此请确保通过添加正确的注释来正确设置您的应用程序。例如,

      @EnableAutoConfiguration
      @EnableWebMvc
      @Configuration
      @ComponentScan
      /*
       * Application Setups using Spring boot.
       */
      public class Application{
      
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }
      

      @EnableWebMvc 是为将 Spring MVC 与 Spring boot 一起使用而添加的注释。 然后你可以像在你的问题中那样定义你的控制器。

      【讨论】:

      • @EnableWebMvc 是 REST 的要求吗?到目前为止我还没有这个注释。
      • 我提醒:到目前为止我还没有设置那个注释。当然,我通过 spring-boot 将它放在类路径中。
      • 是的,如果你想用 Spring boot 设置 MVC,这个注解是必须的。看看这个文档docs.spring.io/spring/docs/current/javadoc-api/org/…
      • 如果我的应用程序配置已经extends SpringBootServletInitializer,是这样吗?那我也不能扩展RepositoryRestMvcConfiguration...
      【解决方案6】:

      将带有控制器类的包添加到主类中的@Component 扫描,例如:@ComponentScan( basePackages = { "your.package.with.controller" } ),当 spring 没有初始化(不知道)控制器时会发生这种情况

      【讨论】:

      • 尝试运行ApplicationContext applicationContext = SpringApplication.run( Application.class, args ); MyServiceRest controller = applicationContext.getBean( MyServiceRest.class );,如果找不到控制器组件扫描有问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 2020-01-02
      • 2015-01-31
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多