什么是Web Services?
Web Services 是应用程序组件
Web Services 使用开放协议进行通信
Web Services 是独立的(self-contained)并可自我描述
Web Services 可通过使用UDDI来发现
Web Services 可被其他应用程序使用
XML 是 Web Services 的基础
它如何工作?
基础的 Web Services 平台是 XML + HTTP。
HTTP 协议是最常用的因特网协议。
XML 提供了一种可用于不同的平台和编程语言之间的语言。
Web services 平台的元素:
SOAP (简易对象访问协议)
UDDI (通用描述、发现及整合)
WSDL (Web services 描述语言)
1,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.5</version>
</dependency>
2,服务端代码
(1)配置类
package com.example.webservice.demo.service.server;
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
/**
* @author LiHaitao
* @description CxfConfig:
* @date 2019/6/17 17:07
**/
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(),"/test/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService demoService() {
return new DemoServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
}
(2)Service接口
package com.example.webservice.demo.service.server;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.JAXBException;
/**
* @author LiHaitao
* @description DemoService:
* @date 2019/6/17 17:05
**/
@WebService(name = "DemoService", // 暴露服务名称
targetNamespace = "http://server.mq.primeton.com"// 命名空间,一般是接口的包名倒序
)
public interface DemoService {
@WebMethod(operationName = "test")//方法名test
public @WebResult(name = "QueryReturn") String test(@WebParam(name = "test")String code);
@WebMethod(operationName = "testXml")//方法名test
public @WebResult(name = "QueryReturn") String testXml(@WebParam(name = "test")String code) throws JAXBException;
}
(3)实现类接口
package com.example.webservice.demo.service.server;
import cn.hutool.json.JSONUtil;
import javax.jws.WebService;
import javax.xml.bind.JAXBException;
/**
* @author LiHaitao
* @description DemoServiceImpl:
* @date 2019/6/17 17:06
**/
@WebService(serviceName = "DemoService", // 与接口中指定的name一致
targetNamespace = "http://server.mq.primeton.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.example.webservice.demo.service.server.DemoService"// 接口地址
)
public class DemoServiceImpl implements DemoService {
/**
* 返回json
* @param orgCode
* @return
*/
@Override
public String test(String orgCode) {
User lihaitao =new User("lihaitao","18");
return JSONUtil.toJsonStr(lihaitao);
}
/**
* 返回xml
* @param orgCode
* @return
*/
@Override
public String testXml(String orgCode) throws JAXBException {
User lihaitao =new User("lihaitao","18");
return JaxResultUtil.objToXmlString(lihaitao);
}
}
(4)实体类User
package com.example.webservice.demo.service.server;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* @author LiHaitao
* @description User:
* @date 2020/1/6 17:51
**/
@XmlRootElement(name = "user")
@XmlType(propOrder = { "name", "age" }) // 定义xml节点的顺序,属性必须在这
public class User {
public User(){
}
public User(String name, String age) {
this.name = name;
this.age = age;
}
private String name;
private String age;
@XmlElement(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "age")
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
(5)工具类
package com.example.webservice.demo.service.server;
import lombok.extern.slf4j.Slf4j;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
/**
* @Description:
* @Author: Lihaitao
* @Date: 2020/1/7 17:22
* @UpdateUser:
* @UpdateRemark:
*/
@Slf4j
public class JaxResultUtil {
private static JAXBContext context = null;
private static Marshaller marshaller = null;
static {
try {
context = JAXBContext.newInstance(User.class);
marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
} catch (JAXBException e) {
log.error("JaxResultUtil init exception", e);
}
}
public static <T> String objToXmlString(T obj) throws JAXBException {
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
}
}
2,客户端
(1)Json格式数据:
@Test
public void test() throws Exception {
JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();
Client client=dcflient.createClient("http://localhost:8081/test/api?wsdl");
Object[] objects1=client.invoke("test","");
System.err.println(objects1[0].toString());
}
(2)xml格式
@Test
public void testXml() throws Exception {
JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();
Client client=dcflient.createClient("http://localhost:8081/test/api?wsdl");
Object[] objects1=client.invoke("testXml","");
System.err.println(objects1[0].toString());
}
代码地址:https://github.com/lihaitao1418064017/webservice-demo.git
来源:CSDN
作者:&Low_Key
链接:https://blog.csdn.net/weixin_38019299/article/details/103879514