项目结构图
1、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<!-- FastJson2 -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier> <!-- 指定jdk版本 -->
</dependency>
<!--webservice cxf-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
</project>
2、CxfConfig.java
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import com.styl.frame.core.service.ServiceDemo;
import com.styl.frame.core.service.UserService;
import com.styl.frame.core.service.impl.ServiceDemoImpl;
import com.styl.frame.core.service.impl.UserServiceImpl;
/**
* @ClassName:CxfConfig
* @Description:cxf发布webservice配置
* @author
* @date:
*/
@Configuration
public class CxfConfig {
@Bean("dispatcherServletCxf")
public ServletRegistrationBean dispatcherServlet(){
//return new ServletRegistrationBean(new CXFServlet(),"/webService/services/*");//发布服务名称
return new ServletRegistrationBean(new CXFServlet(),"/webService/*");//发布服务名称
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
//------------------------接口配置------------(一)----------------------------------------------------------
@Bean
public ServiceDemo ServiceDemo() {
return new ServiceDemoImpl();
}
/** JAX-WS
* 站点服务(一)
*/
@Bean
public Endpoint endpoint_ServiceDemo() {
EndpointImpl endpoint = new EndpointImpl(springBus(), ServiceDemo());
endpoint.publish("/ServiceDemo");
//endpoint.getInInterceptors().add(new WsInterceptor()); //add webservice inteceptor
return endpoint;
}
//------------------------接口配置-----------(二)----------------------------------------------------------
@Bean
public UserService UserService() {
return new UserServiceImpl();
}
/** JAX-WS
* 站点服务(二)
*/
@Bean
public Endpoint endpoint_UserService() {
EndpointImpl endpoint = new EndpointImpl(springBus(), UserService());
endpoint.publish("/UserService");
return endpoint;
}
//--------------------------------------------------------------------------------------------------------
}
3、接口及接口实现类
UserService.java
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* @ClassName:UserService
* @Description:测试服务接口类
* include:两个测试方法
* @author
* @date:
*/
@WebService
public interface UserService {
public String getTs(@WebParam(name = "jsonStr") String jsonStr);
public String getTsUser(@WebParam(name = "jsonStr") String jsonStr);
}
ServiceDemo.java
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface ServiceDemo {
/*@WebMethod //标注该方法为webservice暴露的方法,用于向外公布,它修饰的方法是webservice方法,去掉也没影响的,类似一个注释信息。*/
/*@WebResult(name="out") 可以省略不写*/
public String getValue(@WebParam(name = "param")String param);
/*@WebMethod*/
/*@WebResult(name="out")*/
public String getStatus();
/*@WebMethod*/
/*@WebResult(name="out")*/
public String getUserName(@WebParam(name = "jsonStr") String jsonStr);
}
UserServiceImpl.java
import javax.jws.WebService;
import com.styl.frame.core.service.UserService;
import net.sf.json.JSONObject;
/**
* @ClassName:UserServiceImpl
* @Description:测试服务接口实现类
* @author Maple
* @date:2018年4月10日下午3:58:58
*/
@WebService(endpointInterface="com.styl.frame.core.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
public class UserServiceImpl implements UserService {
public String getTs(String jsonStr) {
return null;
}
public String getTsUser(String jsonStr) {
String result = "";
JSONObject jsr = JSONObject.fromObject(jsonStr);
JSONObject json = new JSONObject();
try {
String datas = jsr.getString("datas");
json.put("datas",datas);
json.put("msg","userServiceInterface");
result = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
ServiceDemoImpl.java
import javax.jws.WebService;
import com.styl.frame.core.service.ServiceDemo;
import net.sf.json.JSONObject;
/*
@WebService(serviceName = "ServiceDemo", // 与接口中指定的name一致,可以省略
targetNamespace = "http://service.xxx.com/", // 与接口中的命名空间一致,一般是接口的包名倒,可以省略
endpointInterface = "com.xxx.service.ServiceDemo"// 接口地址
)
*/
/*
@WebService(serviceName="UserService",//对外发布的服务名
targetNamespace="http://service.core.frame.styl.com",//指定想要的名称空间,通常使用使用包名反转
endpointInterface="com.styl.frame.core.service.UserService")//服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口
*/
/*@WebService(serviceName = "ServiceDemo",targetNamespace = "http://service.core.frame.styl.com/",endpointInterface = "com.styl.frame.core.service.ServiceDemo")*/
@WebService(endpointInterface = "com.styl.frame.core.service.ServiceDemo")
public class ServiceDemoImpl implements ServiceDemo {
public String getValue(String param) {
return null;
}
public String getStatus() {
return null;
}
public String getUserName(String jsonStr) {
String result = "";
JSONObject jsr = JSONObject.fromObject(jsonStr);
JSONObject json = new JSONObject();
try {
String data = jsr.getString("data");
json.put("data",data);
json.put("msg","测试数据!");
result = json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}