简体   繁体   中英

Microserivce Using Spring Boot to Consume SOAP

I need to create a REST service that first consumes SOAP. What would the best way to go about this?

I would like to use Spring Boot to create a microservice, but I have a few questions for those with experience:

  • What other architectures or technologies should I look into using (with spring boot)?
  • Is there a standard technology stack for this?
  • Are there pitfalls I should be aware of?

What other architectures or technologies should I look into using (with spring boot)?

My answer is if you just want to simply provide RESTful service without Spring Cloud, then I think you can refer to following 2 tutorials on Spring official website to achieve this:

Building a RESTful Web Service
Consuming a SOAP web service

Is there a standard technology stack for this?

Currently, I will suggest you to use Spring Boot as your first choice. Because these are rich resources on the web and it does reduces much effort in development.

Are there pitfalls I should be aware of?

If you finally choose Spring Boot, please be familiar to its components, you can start from Guides to realize how it works. Or you may mix up Spring Boot with tradition Spring framework.

There are similar cases in our project and we did it with spring components. As far as i understood, you want to open a REST endpoint which most probably accepts json object and you want to make a soap web service request within that request then you want to return a response containing information from the soap response. To make a soap request, you can use spring web services - WebServiceTemplate. It will marshall your object to xml and make soap request for you. Of course you can use some other web service frameworks like apache cxf which may best fit for some special cases, but i would first try to use a framework which is from spring family while using Spring. You should set timeout values on webservicetemplate object to not wait too long if the external system is not working well or you have some problems with the network. Because it directly affects your systems performance. Also here, i suggest you to implement circuit breaker pattern to make your system more robust. You should always isolate your systems performance from other systems that you integrate and in this scenario you can do this by doing the things explained above.

As per my knowledge , you should use Spring boot application with Maven build. In order to send a REST call to SOAP web service and get back a JSON Response ,you need to follow all of this steps in order :

  1. Business Logic : Mapping the JSON fields such as headers, query-param , body variables to XML Request mandatory fields (either using pojo classes or object-mapper) Reason : Since the SOAP end point will only accept XML Request.

  2. Service Broker Logic : Import "org.springframework.web.client.RestTemplate" and use

     **ResponseEntity<String> responseEntity=RestTemplate.exchange(endPointURL, HttpMethod.GET/POST/PUT/DELETE, HttpEntity/headers, uriVariables)** **endpointURL** -- SOAP End point URL ,that the REST service has to consume. **HTTPMethod** -- Method Type such as GET ,PUT ,POST ,DELETE etc. **HTTPEntity** -- Soap requires for mandatory sender/headers/{a}.Make sure that you set your header name and value as key-Valye pair in HTTP headers. **uriVariables** -- (Object... urivariables) such as String.class ,Integer.class You should also put the **connectTimeout** ,**isSSLDisabled**,**responseCached** elements while generating request to restTemplate. 
  3. responseEntity.getBody() is the XML response after un-marshalling.It can be extracted by using mapper.

      XML_BaseResponse response=mapper.readValue(responseEntity.getBody(), XML_BaseResponse.class); 
  4. Business Logic : Extract necessary field from XML_BaseResponse and using setter's or getter's functions,set the mandatory fields in the response.

      baseResponse.setName(xml_baseResponse.getPersonsName()); baseResponse.setAddress(xml_baseResponse.getAddress()); baseResponse.setCity(xml_baseResponse.getcityName()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM