知优码

您现在的位置是:首页 > 开源项目 > 项目经验

项目经验

PublicCMS集成webservice

2019-11-17项目经验
Public CMS集成webservice
  1. 引入依赖
  2.  
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.2.4</version>
</dependency>
  •  
" org.apache.cxf: cxf-rt-frontend-jaxws: 3.2.4",
" org.apache.cxf: cxf-rt-transports-http: 3.2.4",
  1. 注册servlet
  2. config.initializer. SoapInitializer类
package config.initializer;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
 
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.web.WebApplicationInitializer;
 
public class SoapInitializer implements WebApplicationInitializer {
 
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        Dynamic registration = servletContext.addServlet("soap", new CXFServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping(new String[] { "/soap/*" });
    }
}
  1. 开发接口
  2. com.publiccms.soap.HelloService类
package com.publiccms.soap;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
import org.springframework.stereotype.Component;
 
@WebService
@Component
public class HelloService {
 
    @WebMethod
    public String hello(@WebParam(name = "userName") String userName) {
        return "hello " + userName;
    }
  •  
  1. 注册服务
  2. config.spring. ApplicationConfig,增加
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
    return new SpringBus();
}
 
@Bean
public Endpoint endpoint(SpringBus springBus,HelloService helloService) {
    EndpointImpl endpoint = new EndpointImpl(springBus, helloService);
    endpoint.publish("/hello");
    return endpoint;
  •  
 
  1. 访问服务
  2. http://localhost:8080/publiccms/soap/