如何创建带有弹簧引导REST服务?(How to create a REST service wit

2019-10-21 15:37发布

我使用的是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;
}

Answer 1:

由于您使用的春天启动,请确保您的应用程序是正确设置,通过添加正确的注释。 例如,

@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启动的注解。 然后你可以为你在你的问题做了定义控制器。



Answer 2:

与控制器类添加到包@Component在主类像扫描: @ComponentScan( basePackages = { "your.package.with.controller" } )发生这种情况时弹簧未初始化(不知道)控制器



Answer 3:

你还应该添加URL映射你的方法

@RequestMapping(方法= RequestMethod.GET,值= “url_here”,尝试

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


Answer 4:

在春季启动的最新版本,我目前正在使用,Web服务将地址http://localhost:8080/content

此外,I类用于启动该服务如下所示:

@ComponentScan("eu.buzea")
@EnableAutoConfiguration
@EnableTransactionManagement
@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


Answer 5:

源代码

https://drive.google.com/open?id=0BzBKpZ4nzNzUWmJmOTFwbTFjWWM

使用扬鞭

HTTP://本地主机:7070 /招摇,ui.html#/

**干杯*



Answer 6:

由于当前的spring-boot.1.5.6存在使用不要求cxf

只需使用一个@RestController@GetMapping ,并很高兴地访问localhost:8080/content



文章来源: How to create a REST service with spring-boot?