springboot+webservice

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();
        }
    }
}

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于生产级别的Spring应用程序的框架。它简化了Spring应用程序的配置和部署过程,并提供了一系列开箱即用的功能,包括自动配置、嵌入式服务器和监控等。 Web服务(Web Service)是一种通过网络进行通信的软件系统,它使用标准化的XML消息格式进行数据交换。Web服务可以跨编程语言和操作系统平台进行远程调用,使得不同系统之间的集成更加方便。 在Spring Boot中集成Web服务(WebService),可以使用两种方式进行调用:服务端方式和Java客户端方式。 1. 服务端方式: - 首先,在Spring Boot项目中引入相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` - 然后,创建一个WebService服务端类,使用`@Endpoint`注解标记该类为WebService端点,并使用`@PayloadRoot`注解指定请求的命名空间和请求名称。 - 在WebService服务端类中,定义相应的方法,使用`@ResponsePayload`注解标记方法的返回值作为响应数据。 - 最后,启动Spring Boot应用程序,WebService服务端就可以接收和处理来自客户端的请求。 2. Java客户端方式: - 首先,在Spring Boot项目中引入相关依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` - 然后,创建一个Java客户端类,使用`WebServiceTemplate`类进行远程调用。 - 在Java客户端类中,使用`WebServiceTemplate`的`marshalSendAndReceive`方法发送请求,并指定请求的URL、请求对象和响应对象的类型。 - 最后,启动Spring Boot应用程序,Java客户端就可以调用WebService服务端提供的接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值