springboot发布webservice服务

导入pom

<!--webservice start -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
	<version>3.4.3</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http-jetty</artifactId>
	<version>3.4.3</version>
</dependency>
<!--webservice end -->

application.yml

cxf-config:
  cxfServlet: /services/*

代码

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ttzz.service.GreetWebService;
import com.ttzz.service.HelloService;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;

/**
 * webService 服务
 */
@Configuration
@ConfigurationProperties(prefix = "cxf-config")
public class CxfConfig {

    private String cxfServlet;

    public String getCxfServlet() {
        return cxfServlet;
    }
    public void setCxfServlet(String cxfServlet) {
        this.cxfServlet = cxfServlet;
    }
    @Resource
    private Bus bus;
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), cxfServlet);
    }

    @Resource
    private HelloService helloService;
    @Resource
    private GreetWebService greetWebService;
    @Bean
    public Endpoint helloEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, helloService);
        endpoint.publish("/hello");
        return endpoint;
    }
    @Bean
    public Endpoint greetEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, greetWebService);
        endpoint.publish("/greet");
        return endpoint;
    }
}


package com.ttzz.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(
        name = "HelloWebService", // 暴露服务名称
        targetNamespace = "http://service.ttzz.com"// 命名空间,一般是接口的包名倒序
)
public interface HelloService {
	@WebMethod
	String sayHello(String name);
}


package com.ttzz.service.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Service;

import com.ttzz.service.HelloService;


@Service
@WebService(serviceName = "HelloWebService", // 与接口中指定的name一致, 都可以不写
        endpointInterface = "com.ttzz.service.HelloService" // 接口类全路径
)
public class HelloServiceServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "service=》Good morning : " + name;
    }
}

生成wsdl接口文件路径:http://localhost:8080/services/hello?wsdl

springboot调用webservice服务

通过apach-xcf 生成java代码进行调佣
apache cxf 工具生成java代码
打开 D:\software\apache-cxf\apache-cxf-3.4.4\bin;cmd
wsdl2java  -encoding utf-8 -p comtest.operation -d C:\\Users\\DELL\\Desktop\\new\\operation -impl   -all http://XX/services/operationInfo?wsdl
wsdl2java  -encoding utf-8 -p comtest.operation -d C:\\Users\\DELL\\Desktop\\new\\operation -impl   -all C:\\Users\\DELL\\Desktop\\new\\sdzc\\PackageTraceInfo.xml


-p  指定其wsdl的命名空间,也就是要生成代码的包名:
-d  指定要产生代码所在目录
-encoding 指定编码
-client 生成客户端测试web service的代码
-server 生成服务器启动web  service的代码
-impl 生成web service的实现代码
-ant  生成build.xml文件
-all 生成所有开始端点代码:

解析xml

@Data
@XmlRootElement(name = "Tests")
@XmlAccessorType(XmlAccessType.FIELD)
public class Tests {
    @XmlElement(name = "Test")
    private List<Test> tests;
    private String name;
}
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Test{
	@XmlElement(name = "CODE")
    private String code;
    private Integer type;
    private BigDecimal bigDecimal;
    private Long aLong;
    @XmlElement(name = "n_is")
    private Boolean nIs;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date date;
 
}
import java.text.SimpleDateFormat;
import java.util.Date;
 
import javax.xml.bind.annotation.adapters.XmlAdapter;
 

public class DateAdapter extends XmlAdapter<String, Date> {
    private SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date unmarshal(String v) throws Exception {
        return yyyyMMddHHmmss.parse(v);
    }
    @Override
    public String marshal(Date v) throws Exception {
        return yyyyMMddHHmmss.format(v);
    }
}
package com.ttzz.xmltest;
import java.io.StringReader;
import java.io.StringWriter;
 

import javax.xml.bind.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.*;
import javax.xml.transform.sax.SAXSource;

import org.xml.sax.XMLReader;
 
public class XmlUtils {
    /**
     * bean转成xml
     * @param t :
     * @return string
     */
    public static <T> String beanToXml(T t) throws JAXBException {
        //获得 JAXBContext 类的新实例。参数为类的地址
        JAXBContext context = JAXBContext.newInstance(t.getClass());
        //创建一个可以用来将 java 内容树转换为 XML 数据的 Marshaller 对象。
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xm头声明信息
        //创建一个StringWriter流将接收到的对象流写入xml字符串
        StringWriter sw = new StringWriter();
        //调用marshal方法进行转换
        m.marshal(t,sw);
        //将读取到的StringWriter流转成String返回
        return sw.toString();
    }
 
    /**
     * bean转成xml(泛型使用)
     * @param t :
     * @return java.lang.String
     */
    public static <T> String beanToXml(T t, Class c) throws JAXBException {
        //获得 JAXBContext 类的新实例。参数为类的地址
        JAXBContext context = JAXBContext.newInstance(t.getClass(),c);
        //创建一个可以用来将 java 内容树转换为 XML 数据的 Marshaller 对象。
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xm头声明信息
        //创建一个StringWriter流将接收到的对象流写入xml字符串
        StringWriter sw = new StringWriter();
        //调用marshal方法进行转换
        m.marshal(t,sw);
        //将读取到的StringWriter流转成String返回
        return sw.toString();
    }
 
    /**
     * xml 转成 bean
     * @param xml :
     * @param t :
     * @return T
     */
    public static <T> T xmlToBean(String xml, T t) throws JAXBException {
        //获得 JAXBContext 类的新实例。参数为类的地址
        JAXBContext context = JAXBContext.newInstance(t.getClass());
        //创建一个可以用来将 XML 数据转换为 java 内容树的 Unmarshaller 对象。
        Unmarshaller um = context.createUnmarshaller();
        //创建一个StringReader将xml报文转成流
        StringReader sr = new StringReader(xml);
        //调用unmarshal进行转换,并把Object类型强转为调用者的类型
        t = (T) um.unmarshal(sr);
        //将对象返回给调用者
        return t;
    }
 
    /**
     * xml 转成 bean(泛型使用)
     * @param t, t
     * @param xml xml
     * @param c t]
     * @return T
     */
    public static <T> T xmlToBean(String xml, T t, Class c) throws JAXBException {
        //获得 JAXBContext 类的新实例。参数为类的地址
        javax.xml.bind.JAXBContext context = JAXBContext.newInstance(t.getClass(),c);
        //创建一个可以用来将 XML 数据转换为 java 内容树的 Unmarshaller 对象。
        Unmarshaller um = context.createUnmarshaller();
        //创建一个StringReader将xml报文转成流
        StringReader sr = new StringReader(xml);
        //调用unmarshal进行转换,并把Object类型强转为调用者的类型
        t = (T) um.unmarshal(sr);
        //将对象返回给调用者
        return t;
    }
 
}
package com.ttzz.xmltest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBException;

public class TestCase {
	public static void main(String[] args) {
        List<Test> testList = new ArrayList<>();
        Test test = new Test();
        test.setCode("code");
        test.setType(1);
        test.setBigDecimal(new BigDecimal("1.01"));
        test.setALong(1L);
        test.setNIs(true);
        test.setDate(new Date());
        testList.add(test);
 
        Test test1 = new Test();
        test1.setCode("code1");
        test1.setType(2);
        test1.setBigDecimal(new BigDecimal("2.01"));
        test1.setALong(2L);
        test1.setNIs(false);
        test1.setDate(new Date());
        testList.add(test1);
 
        Tests tests = new Tests();
        Tests tests2 = new Tests();
        tests.setName("测试名称");
        tests.setTests(testList);
        System.out.println("数据组装完成的对象bean为:\n" + tests);
        try {
            System.out.println("================== bean转成xml格式  ======================");
            String xml = XmlUtils.beanToXml(tests);
            System.out.println("bean转成xml格式为:\n" + xml);
            System.out.println("=================== xml转成bean格式 =====================");
            tests2 = XmlUtils.xmlToBean(xml,tests2);
            System.out.println("xml转成bean格式为:\n" + tests2);
        } catch (JAXBException ex) {
            ex.printStackTrace();
        }
    }
}