webservice服务端demo
1. 定义输入输出实体
输入实体 InputEntity
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "InputEntity",
propOrder = {"id", "data"}
)
public class InputEntity {
@XmlElement(
name = "id",
required = true
)
protected Integer jszt;
@XmlElement(
name = "data",
required = true
)
protected Object data;
public InputEntity() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id= id;
}
public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data= data;
}
}
输出实体 OutputEntity
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "OutputEntity",
propOrder = {"code", "msg"}
)
public class OutputEntity {
@XmlElement(
name = "code",
required = true
)
protected Integer jszt;
@XmlElement(
name = "msg",
required = true
)
protected String msg;
public OutputEntity() {
}
public Integer getCode() {
return this.code;
}
public void setCode(Integer code) {
this.code= code;
}
public String getMsg() {
return this.msg;
}
public void setMsg(String msg) {
this.msg= msg;
}
}
2. 定义暴露的接口 myService.interface
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
@WebService(
name = "myService",
targetNamespace = "http://test.ws.com"
)
@SOAPBinding(
parameterStyle = ParameterStyle.BARE
)
public interface myService {
@WebMethod(
operationName = "getInfo"
)
OutputEntity getInfo(@WebParam(name = "item",targetNamespace = "http://test.ws.com") Object item);
}
3. 定义具体实现类 myServiceImpl.class
import com.ws.test.utils.JsonUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.logging.Logger;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(
serviceName = "myService",
name = "myService",
portName = "myPortName",
targetNamespace = "http://test.ws.com",
endpointInterface = "com.ws.test.myService"
)
public class myServiceImpl implements myService {
private static final Logger logger = Logger.getLogger(myServiceImpl.class.getName());
public myServiceImpl() {
}
public OutputEntity getInfo(@WebParam(name = "item",targetNamespace = "http://test.ws.com") InputEntity item) {
String result = "数据为空,请重新发送!";
OutputEntity outres = new OutputEntity();
outres.setCode(-1);
if (null != outres) {
result = "数据接收成功,提交线程处理中!";
outres.setCode(0);
}
String str = "id: " + item.getId() + "data: " + item.getData();
logger.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " <<<< Server >>>> received parameters: " + str);
outres.setMsg(result);
return outres;
}
}
4. 定义发布类 Config.class
import com.ws.test.myService;
import com.ws.test.myServiceImpl;
import javax.xml.ws.Endpoint;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
public Config() {
}
@Bean(
name = {"cxf"}
)
public SpringBus springBus() {
return new SpringBus();
}
@Bean(
name = {"wsBean"}
)
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), new String[]{"/ws/*"});
return wbsServlet;
}
@Bean
public myService myService() {
return new myServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(this.springBus(), new myServiceImpl());
//发布wsdl文件
endpoint.publish("/mywsdl");
return endpoint;
}
}
↑↑ 服务端代码编写完,可以写一个Controller类模拟客户端向服务端接口发送请求自测一下 ↓↓
import com.ws.test.utils.JsonUtils;
import com.ws.test.workorder.yxfk.InputEntity;
import com.ws.test.OutputEntity;
import com.ws.test.myService;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping({"/test"})
public class testController {
private static final Logger logger = Logger.getLogger(testController.class.getName());
public testController() {
}
@RequestMapping({"/send.do"})
public String sendInfo(@RequestBody InputEntity item) throws MalformedURLException {
URL url = new URL("http://localhost:8080/ws/mywsdl?wsdl");
QName qName = new QName("http://test.ws.com", "myService");
Service service = Service.create(url, qName);
myService ms = (myService)service.getPort(new QName("http://test.ws.com", "myPortName"), myService.class);
OutputEntity out = ms.getInfo(item);
//String res = JsonUtils.getJson(out);
String res = "code: " + out.getCode() + "msg: " + out.getMsg();
return res;
}
}
也可以使用JDK自带wsimport.exe根据wsdl文件生成客户端代码
- cd到jdk的bin目录下,wsimport.exe在bin目录下
- 创建一个用来存放要生成客户端代码的文件夹,例如我创建的是d:\webservice\src
- 执行命令
wsimport -s d:\webservice\src -keep -p com.ws.test.client -verbose -Xnocompile d:\webservice\mywsdl.wsdl
参数说明:
-s:指定java文件的输出目录(代码生成在哪个文件夹,需要提前创建好)
-keep:是否生成java源文件
-p:定义生成类的包名,不定义则默认生成
-verbose:在控制台显示输出信息
-Xnocompile:表示不需要编译
最后 d:\webservice\mywsdl.wsdl 表示wsdl文件的存放位置,可以替换成发布的wsdl地址如 http://localhost:8080/ws/myservice?wsdl
使用客户端代码编写调用方法
可以新启一个Springboot项目,导入前面生成的客户端代码文件,写一个Controller类来测试调用服务端的方法,代码大致如下:
import com.ws.client.*;
import com.ws.utils.JsonUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/client")
public class ClientController {
@RequestMapping("/test")
public String test(@RequestBody InputEntityArray inList){
myServiceImpl impl = new myServiceImpl();
myService service = impl.getInfoPortName();
OutputEntity out = service.getInfo(inList);
String res = JsonUtils.getJson(out);//将结果转换为字符串
return res;
}
}
JsonUtils工具类:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
public static String getJson(Object object) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}