Spring工程集成CXF實現WebService接口

第一步:引入cxf框架相關的jar文件,引入方式(maven),再pom.xml中添加 :web

<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-transports-http</artifactId>
   <version>3.1.10</version>
</dependency>
<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>3.1.10</version>
</dependency>

第二步:在web.xml中添加:spring

<servlet>
   <description>CXF Endpoint</description>
   <display-name>cxf</display-name>
   <servlet-name>cxf</servlet-name>
   <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
   <servlet-name>cxf</servlet-name>
   <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

第三步:建立接口文件apache

@WebService
public interface OaService {
    /**
     * 登陸接口
     * @return
     */
    public String login(String name, String password);
}
@WebService(endpointInterface = "com.weichai.modules.webService.OaService", serviceName = "oaService")
@Component
public class OaServiceImpl implements OaService {
    private Logger logger = LoggerFactory.getLogger(OaServiceImpl.class);

    /**
     * 登陸接口
     * @return
     */
    public String login(String name, String password){
        JSONObject rData = new JSONObject();
        try {
            rData.put("status", true);
            rData.put("name", name);
            rData.put("password", password);
        }catch (Exception e){
            rData.put("status", false);
            logger.error("[oaService login]", e);
        }
        return rData.toJSONString();
    }
}

第四步:在spring-content.xml中添加:app

<!-- webService 接口 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="oaService" implementor="com.weichai.modules.webService.impl.OaServiceImpl" address="/oaService" ></jaxws:endpoint>

第五步:啓動服務,訪問:http://localhost:8080/ws/oaService?wsdl框架

PS:"?wsdl"不能省略。frontend

相關文章
相關標籤/搜索