英文:
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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论