cxf发布webservce的2种方式
一.使用配置文件
1.配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-context*.xml</param-value>
</context-param>
<!-- cxf servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
2.配置spring-context-webservice.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxws:endpoint implementor="com.lmp.webservice.impl.DataWebServiceImpl" address="/data">
</jaxws:endpoint>
</beans>
3.接口类
package com.lmp.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface DataWebService {
/**
* 获取用户信息
* @param userId 用户编号
* @return JSON字符串
*/
@WebMethod
String getUser(@WebParam(name="userId")String userId);
}
4.实现类
package com.lmp.webservice.impl;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.druid.support.json.JSONUtils;
import com.lmp.service.UserService;
import com.lmp.webservice.DataWebService;
@WebService(endpointInterface="com.lmp.webservice.DataWebService")
public class DataWebServiceImpl implements DataWebService {
@Autowired
private UserService userService;
/**
* 获取用户信息
* @param userId 用户编号
* @return JSON字符串
*/
@Override
public String getUser(String userId) {
User user= userService.getUserByUserId(userId);
Map<String, String> map = new HashMap<>();
map.put("name", user.getUserName());
map.put("sex", user.getUserSex());
return JSONUtils.toJSONString(map);
}
}
5.发布路径:http://localhost:8080/PP/ws/data?wsdl
如下则发布成功:
二.使用spring注入
1.其他同方法一,仅仅不使用spring-context-webservice.xml
WebServiceConfig.java
package com.lmp.common.config;
import com.lmp.webservice.DataWebService;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {
@Autowired
private DataWebService dataWebServce;
/**
* 发布服务webservice
* @return
*/
@Bean
public Endpoint createEndpoint(){
return EndpointImpl.publish("/data", dataWebServce);
}
}