Web Services 如何从Sping Boot 调用SOAP服务,而无需使用JAXB进行编组和解组?

tv6aics1  于 2023-05-18  发布在  其他
关注(0)|答案(1)|浏览(186)

我有一个.jar包,它是从SOAP服务war包的WSDL生成的。SOAP服务没有等效的JAXB兼容包来使用JAXB通过WebServiceTemplate进行编组和解组。
我正在使用下面的代码来调用具有等效JAXB包的SOAP服务,但是现在我没有JAXB等效SOAP服务包来在SOAP客户端应用程序中调用,那么如何调用SOAP服务呢?
PersonActivityList.class

package com.test.jee.cxfws;

@XmlAccessorType(XmlAccessorType.Field)
@XmlType(name = "personActivityList", proOrder = {"request"})

public class PersonActivityList{
    protected PersonActionRequest request;
    
    public PersonActivityList(){}
    
    public PersonActionRequest getRequest() {return this.request;}
    
    public void setRequest(PersonActionRequest var1) {this.request = var1;}
}

app.yml

personactivity:
uri: https:test.com/personactivity/V4
schema-path: "com.test.jee.cxfws"
wsdlVersion: 4.0
appId: pdv

PersonConfig.java

@Configuration
public class PersonConfig {

    @Value("${personactivity.uri}")
    String defaultUri;

    @Value("${personactivity.schema-path}")
    String schemaPath;

    // specifically the HTTP message sending functionality
    // sets up the HTTP client with appropriate connection settings, including timeouts and SSL/TLS configuration
    @Autowired
    TestConfig testConfig;

    // is an implementation of a client interceptor
    // this interceptor is responsible for adding custom headers to the outgoing requests in a web service client based on the values provided in the appId and userId properties.
    @Autowired
    TESTHeaderInterceptor headerInterceptor;

    Jaxb2Marshaller getJaxb2Marshaller() {
        Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
        jaxb2Marshaller.setContextPath(schemaPath);
        return jaxb2Marshaller;
    }
    
    @Bean ("PersonWebServiceTemplate")
    public WebServiceTemplate updateWebServiceTemplate() throws Exception {
        SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(MessageFactory.newInstance());
        messageFactory.afterPropertiesSet();
        WebServiceTemplate updateWebServiceTemplate = new WebServiceTemplate(messageFactory);
         updateWebServiceTemplate.setMarshaller(getJaxb2Marshaller());
        updateWebServiceTemplate.setUnmarshaller(getJaxb2Marshaller());
        updateWebServiceTemplate.setDefaultUri(defaultUri);
        updateWebServiceTemplate.setMessageSender(testConfig.createMessageSender());
        updateWebServiceTemplate.afterPropertiesSet();
        updateWebServiceTemplate.setInterceptors(new ClientInterceptor[]{headerInterceptor});
        return updateWebServiceTemplate;
    }

    public String getDefaultUri() {
        return defaultUri;
    }
}

PersonActivity.java

@Service
public class PublicActivity{
    @Autowired
    private WebServiceTemplate PersonWebServiceTemplate;
    
    public PersonActivityListResponse getPersonActivityList(PersonActivityList request) {
            PersonActivityListResponse response = (PersonActivityListResponse) PersonWebServiceTemplate.marshalSendAndReceive(request);
            return response;

    }
}

如何在不使用JAXB的情况下编写调用SOAP服务的等效代码?

htzpubme

htzpubme1#

我使用了下面的代码,它工作了。

@Service
public class PublicActivity {
    @Autowired
    private WebServiceTemplate PersonWebServiceTemplate;

    public PersonActivityListResponse getPersonActivityList(PersonActivityList request) {

        JAXBElement<PersonActivityList> jaxbElement = new ObjectFactory().createPersonActivityList(request);
        JAXBElement<PersonActivityListResponse> response =
                (JAXBElement<PersonActivityListResponse>) PersonServiceTemplate.marshalSendAndReceive(jaxbElement);
        return response.getValue();

    }
}

相关问题