openFein 调用webservice,soap+xml

openFein 调用webservice,soap+xml,第1张

openFein 调用webservice,soap+xml

一、服务端:

1.1目录结构:

1.2maven依赖



    4.0.0

    org.example
    soapdemo
    1.0-SNAPSHOT

    
        8
        8
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.2.5
        
        
            org.apache.cxf
            cxf-rt-features-logging
            3.2.5
        
        
            org.hibernate
            hibernate-validator
            5.2.4.Final
        

    

1.3WebServiceConfig配置

package com.soap.config;


import com.soap.service.StudentService;
import org.apache.cxf.Bus;
import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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


@Configuration
public class WebServiceConfig {

    @Resource
    private Bus bus;

    @Resource
    private StudentService studentService;

    @Bean
    public Endpoint endpointStudentService() {
        EndpointImpl endpoint=new EndpointImpl(bus,studentService);
        endpoint.getInInterceptors()
                .add(loggingInInterceptor());
        endpoint.getOutInterceptors()
                .add(loggingOutInterceptor());
        endpoint.publish("/studentService");
        return endpoint;

    }

    @Bean
    public LoggingInInterceptor loggingInInterceptor() {
        return new LoggingInInterceptor();
    }

    @Bean
    public LoggingOutInterceptor loggingOutInterceptor() {
        return new LoggingOutInterceptor();
    }
}

1.3Student实体类

package com.soap.entity;

public class Student {
    private String stuName;
    private Integer stuAge;

    public Student() {}

    public Student(String stuName, Integer stuAge) {
        this.stuName = stuName;
        this.stuAge = stuAge;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
}

1.4提供服务类

package com.soap.service;

import com.soap.entity.Student;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.List;

@WebService(targetNamespace = "http://service.soap.com")//  命名空间,建议包名倒写
public interface StudentService {

    @WebMethod
    List getStudentList();
}

1.5服务实现类

package com.soap.service.impl;

import com.soap.entity.Student;
import com.soap.service.StudentService;
import org.springframework.stereotype.Component;
import javax.jws.WebService;
import java.util.Arrays;
import java.util.List;


@WebService(serviceName = "StudentService",
        targetNamespace = "http://service.soap.com",
        endpointInterface = "com.soap.service.StudentService")
@Component
public class StudentServiceImpl implements StudentService {
    @Override
    public List getStudentList() {
        Student stu1 = new Student("学生1",25);
        Student stu2 = new Student("学生2",30);
        return Arrays.asList(stu1,stu2);
    }
}

1.6启动类

package com.soap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SoapBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SoapBootApplication.class,args);
    }
}

1.7 application.yml文件

server:
  port: 8081
  servlet:
    context-path: /

1.8启动后访问地址:http://localhost:8081/services/studentService?wsdl

1.9====================服务端配置完成================================

二、客户端

2.1目录结构

 2.1maven依赖,其他依赖自行添加


            io.github.openfeign
            feign-jaxb
        

        
            io.github.openfeign
            feign-jaxrs
        
        
            io.github.openfeign
            feign-soap
            10.2.0
        
        
            io.github.openfeign
            feign-httpclient
            10.10.1
        


        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

2.2ConvertConfig配置类(解析xml)

package com.example.consumer.config;


import feign.Logger;
import feign.Util;
import feign.codec.Decoder;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.jaxb.JAXBContextFactory;
import org.springframework.context.annotation.Bean;
import org.w3c.dom.document;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.documentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.*;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.ws.soap.SOAPFaultException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static javax.xml.soap.SOAPConstants.DEFAULT_SOAP_PROTOCOL;

public class ConvertConfig {
    private Charset charsetEncoding = StandardCharsets.UTF_8;
    private String soapProtocol = DEFAULT_SOAP_PROTOCOL;

    private static final JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder()
            .withMarshallerJAXBEncoding("UTF-8")
            .build();

    @Bean
    public Encoder feignEncoder() {
        return (object, bodyType, template) -> {
            if (!(bodyType instanceof Class)) {
                throw new UnsupportedOperationException(
                        "SOAP only supports encoding raw types. Found " + bodyType);
            }
            try {
                document document = documentBuilderFactory.newInstance().newdocumentBuilder().newdocument();
                Marshaller marshaller = jaxbContextFactory.createMarshaller((Class) bodyType);
                marshaller.marshal(object, document);
                SOAPMessage soapMessage = MessageFactory.newInstance(soapProtocol).createMessage();
                SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
                soapEnvelope.addNamespaceDeclaration("ser", "http://service.soap.com");
                soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charsetEncoding.displayName());
                soapMessage.getSOAPBody().adddocument(document);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                soapMessage.writeTo(bos);
                template.body(new String(bos.toByteArray()).replaceAll("getStudentList", "ser:getStudentList"));
            } catch (SOAPException | JAXBException | ParserConfigurationException|IOException| TransformerFactoryConfigurationError e) {
                throw new EncodeException(e.toString(), e);
            }
        };
    }

    @Bean
    public Decoder feignDecoder() {
        return (response, type) ->{
            try {
                if (response.status() != 404 && response.status() != 204) {
                    if (response.body() == null) {
                        return null;
                    } else {
                        SOAPMessage message = MessageFactory.newInstance(soapProtocol).createMessage(null,response.body().asInputStream());
                        SOAPBody soapBody = message.getSOAPBody();
                        if (soapBody != null) {
                            if (message.getSOAPBody().hasFault()) {
                                throw new SOAPFaultException(message.getSOAPBody().getFault());
                            }
                            //重要!!!!
                            Class tt =(Class)type;
                            Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller(tt);

                            return unmarshaller.unmarshal(soapBody.extractContentAsdocument(),tt).getValue();
                        }
                    }
                } else {
                    return Util.emptyValueOf(type);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return Util.emptyValueOf(type);
        };
    }

    @Bean
    feign.Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

2.3GetStudentList实体类

package com.example.consumer.soap.entity;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class GetStudentList {
}

2.4GetStudentListResponse实体类

package com.example.consumer.soap.entity;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "getStudentListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class GetStudentListResponse {
    @XmlElement(name = "return")
    private List students;

    public List getStudents() {
        return students;
    }

    public void setStudents(List students) {
        this.students = students;
    }
}

2.5 Student实体类

package com.example.consumer.soap.entity;

public class Student {
    private int stuAge;
    private String stuName;

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }
}

2.6 feign远程调用类

package com.example.consumer.soap.feign;

import com.example.consumer.config.ConvertConfig;
import com.example.consumer.soap.entity.GetStudentList;
import com.example.consumer.soap.entity.GetStudentListResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;


@FeignClient(name = "SoapFeign",url = "http://localhost:8081",configuration = {ConvertConfig.class})
public interface SoapFeign {
    @PostMapping(value = "/services/studentService",consumes = {MediaType.TEXT_XML_VALUE})
    public GetStudentListResponse getStudentList(GetStudentList getStudentList);
}

2.7 启动类

package com.example.consumer;

import com.alibaba.fastjson.JSON;

import com.example.consumer.soap.entity.GetStudentListResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPMessage;
import java.io.ByteArrayInputStream;

@SpringBootApplication
@EnableFeignClients(basePackages = {"com.example.consumer.soap.feign"})
public class ConsumerApplication {
    public static void main(String[] args){
        ConsumerApplication consumerApplication=new ConsumerApplication();
//        try {
//            consumerApplication.method();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        SpringApplication.run(ConsumerApplication.class, args);
    }
    //可进行测试
    public void method() throws Exception {
        String example ="n" +
                "   n" +
                "      n" +
                "         n" +
                "            25n" +
                "            学生1n" +
                "         n" +
                "         n" +
                "            30n" +
                "            学生2n" +
                "         n" +
                "      n" +
                "   n" +
                "";
        SOAPMessage message = MessageFactory.newInstance().createMessage(null,
                new ByteArrayInputStream(example.getBytes()));
        Unmarshaller unmarshaller = JAXBContext.newInstance(GetStudentListResponse.class).createUnmarshaller();
        JAXBElement unmarshal = unmarshaller.unmarshal(message.getSOAPBody().extractContentAsdocument(), GetStudentListResponse.class);
        GetStudentListResponse value = unmarshal.getValue();
        System.out.println(JSON.toJSonString(value));
    }

}

2.8 开启feign日志

logging:
  level:
    com.example.consumer.soap.feign.SoapFeign: debug

2.9 controller类

  @Resource
    private SoapFeign soapFeign;
    @GetMapping("/soap")
//R类,可以用Map代替
    public R testSoap() {
        GetStudentListResponse response= soapFeign.getStudentList(new GetStudentList());
        return R.success().put("students:",response);
    }

3.0 xml或wsdl文件























 


 



 
 



















3.1可用soapUI(soap请求,推荐)、postman(post请求)进行测试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5661303.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存