springboot同时提供webservice和restful

@Configuration
public class CxfConfig {
    @Autowired
    @Qualifier(Bus.DEFAULT_BUS_ID)
    private SpringBus bus;

    @Autowired
    QueryService queryService;

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
//        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
//        CXFServlet cxfServlet = new CXFServlet();
//        registrationBean.setServlet(cxfServlet);
//        List urlMappings = new ArrayList<>();
//        urlMappings.add("/services/*");
//        registrationBean.setUrlMappings(urlMappings);
//        registrationBean.setName("cxfDispatcherServlet");
//        registrationBean.setLoadOnStartup(1);
//        return registrationBean;
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");

    }

    /**
     * 同时支持rest请求的Servlet,rest请求以“api”为url前缀
     * @return
     */
    @Bean
    public ServletRegistrationBean dispatcherRestServlet() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        //替换成自己想买的controller包路径
        context.scan("com.bocom.controller");
        DispatcherServlet disp = new DispatcherServlet(context);
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(disp);
        registrationBean.setLoadOnStartup(1);
        //映射路径自定义
        registrationBean.addUrlMappings("/api/*");
        registrationBean.setName("rest");
        return registrationBean;
    }

    @Bean
    public EndpointImpl queryEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, queryService);
        endpoint.publish("/TmriOutAccess");
        return endpoint;
    }

}

你可能感兴趣的:(cxf,springboot,restful)