如何在Spring Boot中调用SOAP服务,而不使用JAXB进行编组和解组?

huangapple go评论56阅读模式
英文:

How to call SOAP service from Spring Boot without marshalling and unmarshalling using JAXB?

问题

You can call a SOAP service without using JAXB by manually creating SOAP requests and parsing SOAP responses. Here's an example of how you can modify your code to achieve this:

import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;

public class CustomSoapClient extends WebServiceGatewaySupport {

    public CustomSoapClient() {
        SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
        messageFactory.afterPropertiesSet();
        setMessageFactory(messageFactory);
    }

    public String callSoapService(String soapRequest) {
        // You need to create a SOAP request string and pass it as a parameter.
        // This should include the SOAP envelope, headers, and body as needed.

        SoapMessage soapResponse = sendAndReceive(message -> {
            // Configure the SOAP request message, including the endpoint URI.
            message.setSoapAction("SOAP_ACTION"); // Set the appropriate SOAP action
            message.setPayloadText(soapRequest);
        });

        // Parse the SOAP response as needed.
        String soapResponseString = soapResponse.toString(); // Convert the response to a string

        // Process the soapResponseString as required for your application.

        return soapResponseString;
    }
}

In this code, you create a custom SOAP client using Spring Web Services. You manually construct the SOAP request, set the appropriate SOAP action, and send it to the SOAP service. Then, you parse and process the SOAP response as needed.

Please note that constructing SOAP requests manually can be error-prone and may require a good understanding of the SOAP protocol and the specific requirements of the SOAP service you are calling. Additionally, you will need to handle any authentication, error handling, and other aspects of the SOAP communication yourself.

英文:

I have a .jar package that I have generated from WSDL of the SOAP service war package. SOAP service doesn't have an equivalent JAXB-compatible package to use JAXB for marshalling and unmarshalling with WebServiceTemplate.

I am using the following code to call SOAP service with an equivalent JAXB package but right now I don't have JAXB equivalent SOAP service package to call in the SOAP client application then how to call SOAP service?

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;

	}
}

How to write the equivalent code to call SOAP service without using JAXB?

答案1

得分: 0

我使用下面的代码它有效

    @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();
    
    
        }
    }
英文:

I used below code and it worked.

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


    }
}

huangapple
  • 本文由 发表于 2023年5月17日 08:56:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定