Spring Boot SOAP Response to JSON - Any plugin or any logic - spring

Need to convert SOAP Response in SpringBoot (WebServivetemplate) to REST JSON.
Currently SOAP Response is working fine...need to convert this as JSON input /output. Any logic in spring framework other than looping through XML document node by node.

follwoing is good example how can you directly convert it using jaxb
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
#Bean
public Jaxb2Marshaller marshaller(){
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the <generatePackage> specified in
// pom.xml
marshaller.setContextPath("phoneverify.wsdl");
return marshaller;
}
https://github.com/kennymarlo-private/spring-boot-rest-consume-soap/blob/master/src/main/java/com/example/wsdl/wsdl/demo/config/WsdlConfiguration.java

Related

How to use Protobuf and JSON API simultaneously on SpringBoot?

My project only had Protobuf APIs, but now it has to provide JSON APIs, too.
protobufHttpMessageConverter was applied to all controllers by configuration.
#Configuration
class APIConfig : WebMvcConfigurer {
#Bean
fun protobufHttpMessageConverter(): ProtobufHttpMessageConverter {
val converter = ProtobufHttpMessageConverter()
converter.defaultCharset = StandardCharsets.UTF_8
return converter
}
}
But now it should be applied to some controllers that provide Protobuf APIs, and should not be applied to other controllers that provide JSON APIs.
Does SpringBoot have that kind of flexibility?

SpringBoot controller returns XML although content-type set to JSON explicitly

I have 2 spring boot REST-services which interact as producer and consumer.
One is producer of JSON content and the other is the consumer.
In the consumer service I use Spring's RestTemplate to invoke the producer's endpoint and get the result.
Code
Producer's relevant code for controller's POST endpoint is as follows:
#RequestMapping(value = "/cars", method = RequestMethod.POST)
public ResponseEntity<Cars> getCars(/* methods params */) {
// some code here
HttpHeaders respHeader = new HttpHeaders();
respHeader.set("Content-Type", "application/json");
ResponseEntity<Cars> resp = new ResponseEntity<Cars>(cars, respHeader, HttpStatus.OK);
return resp;
}
Notice in the producer code above:
I don't have produces="application/json" specified explicitly in the RequestMapping annotation.
However, I set content-type as "application/json" in the response headers.
Issue
But consumer gets the response in XML instead of JSON.
Jackson is there in the class path.
Questions
What explains returning XML and not JSON even though content-type explicitly set in response-headers to "application/json"?
Assume the response-header doesn't have any effect, what is the default - is it XML instead of JSON?
You can configure the default content-type for any response by customizing Spring's WebMvcConfigurer.
To do so add following code to a given configuration class that is annotated with #Configuration:
#Bean
public WebMvcConfigurer customConfigurer() {
return new WebMvcConfigurer() {
#Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
};
}
You could set a breakpoint in Spring's ContentTypeDelegatingMessageConverter and debug to see the resolved message-converter for your response.
Content Negotiation
The concept and process between a web-client and -server determining the content-type of a response is called Content-Negotiation.
Since Spring MVC the registered WebMvcConfigurer and HttpMessageConverters are responsible for the representation and content-type of the response.
In SpringBoot those are autoconfigured.
For any #RestController or a controller-endpoint having #ResponseBody the default content-type of the response is XML, if Jackson is available in the classpath and auto-configured successfully, the default content-type should be JSON.
See also:
tutorial on Baeldung: Spring MVC Content Negotiation
How to make spring boot default to application/json;charset=utf-8 instead of application/json;charset=iso-8859-1
It appears that the order of HttpMessageConverters matters. I encountered the same problem with default xml responses when there is a transitive xml dependency. Adding MappingJackson2HttpMessageConverter to the top of converters list solved it for me.
For some reason configurer.defaultContentType(MediaType.APPLICATION_JSON) this suggestion from previous answers breaks apis that produce media types other than json (when requests does not contain Accept header). These apis started returning http status 406 with json as the defaultContentType
My Spring Boot version is 2.7.1
Here is the configuration class:
#Configuration
#RequiredArgsConstructor
public class WebMvcConfiguration implements WebMvcConfigurer {
private final ObjectMapper objectMapper;
#Override
public void extendMessageConverters(#NotNull List<HttpMessageConverter<?>> converters) {
converters.add(0, new MappingJackson2HttpMessageConverter(objectMapper));
}
}
When there is no default content type defined, xml becomes the default since spring iterates over converters to decide produceable content types. And since xml converter is added before json converter it uses xml. Adding json converter ahead of xml converter fixes this.

Additional Media Types for Spring Data Rest Repository Controllers

Trying to add the JSON-LD serialisation provided by the project Hydra Java to an application using Spring Data Rest and the controllers generated by #RepositoryRestResource
While I've been able to serialize as JSON-LD using manually created #RepositoryRestController using the application/ld+json media type, I'm unable to do the same for the controllers generated for the repositories annotated as RepositoryRest Resource. They seem to just accept application/json and application/hal+json
Is it possible to configure these controllers to also accept additional media types?
This is how I've configured the HydraMessageConverter provided by the Hydra Java project:
#Bean
public RepositoryRestConfigurer repositoryRestConfigurer()
{
return new RepositoryRestConfigurerAdapter()
{
#Override
public void configureHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(0, new HydraMessageConverter());
super.configureHttpMessageConverters(messageConverters);
}
}
}

Spring Boot with Two MVC Configurations

I have a Spring Boot app with a REST API, using Jackson for the JSON view configuration. It works great and I can get all the Spring Boot goodness.
However, I need to add an additional REST API that is similar but with different settings. For example, among other things, it needs a different Jackson object mapper configuration because the JSON will look quite a bit different (e.g. no JSON arrays). That is just one example but there are quite a few differences. Each API has a different context (e.g. /api/current and /api/legacy).
Ideally I'd like two MVC configs mapped to these different contexts, and not have to give up any of the automatic wiring of things in boot.
So far all I've been able to get close on is using two dispatcher servlets each with its own MVC config, but that results in Boot dropping a whole bunch of things I get automatically and basically defeats the reason for using boot.
I cannot break the app up into multiple apps.
The answer "you cannot do this with Boot and still get all its magic" is an acceptable answer. Seems like it should be able to handle this though.
There's several ways to achieve this. Based on your requirement , Id say this is a case of managing REST API versions.
There's several ways to version the REST API, some the popular ones being version urls and other techniques mentioned in the links of the comments.
The URL Based approach is more driven towards having multiple versions of the address:
For example
For V1 :
/path/v1/resource
and V2 :
/path/v2/resource
These will resolve to 2 different methods in the Spring MVC Controller bean, to which the calls get delegated.
The other option to resolve the versions of the API is to use the headers, this way there is only URL, multiple methods based on the version.
For example:
/path/resource
HEADER:
X-API-Version: 1.0
HEADER:
X-API-Version: 2.0
This will also resolve in two separate operations on the controller.
Now these are the strategies based on which multiple rest versions can be handled.
The above approaches are explained well in the following: git example
Note: The above is a spring boot application.
The commonality in both these approaches is that there will need to be different POJOS based on which Jackson JSON library to automatically marshal instances of the specified type into JSON.
I.e. Assuming that the code uses the #RestController [org.springframework.web.bind.annotation.RestController]
Now if your requirement is to have different JSON Mapper i.e. different JSON mapper configurations, then irrespective of the Spring contexts you'll need a different strategy for the serialization/De-Serialization.
In this case, you will need to implement a Custom De-Serializer {CustomDeSerializer} that will extend JsonDeserializer<T> [com.fasterxml.jackson.databind.JsonDeserializer] and in the deserialize() implement your custom startegy.
Use the #JsonDeserialize(using = CustomDeSerializer.class) annotation on the target POJO.
This way multiple JSON schemes can be managed with different De-Serializers.
By Combining Rest Versioning + Custom Serialization Strategy , each API can be managed in it's own context without having to wire multiple dispatcher Servlet configurations.
Expanding on my comment of yesterday and #Ashoka Header idea i would propose to register 2 MessageConverters (legacy and current) for custom media types. You can do this like that:
#Bean
MappingJackson2HttpMessageConverter currentMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
// set features
jsonConverter.setObjectMapper(objectMapper);
jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("json", "v2")));
return jsonConverter;
}
#Bean
MappingJackson2HttpMessageConverter legacyMappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
// set features
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
Pay attention to the custom media-type for one of the converters.
If you like , you can use an Interceptor to rewrite the Version-Headers proposed by #Ashoka to a custom Media-Type like so:
public class ApiVersionMediaTypeMappingInterceptor extends HandlerInterceptorAdapter {
#Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
try {
if(request.getHeader("X-API-Version") == "2") {
request.setAttribute("Accept:","json/v2");
}
.....
}
}
This might not be the exact answer you were looking for, but maybe it can provide some inspiration. An interceptor is registered like so.
If you can live with a different port for each context, then you only have to overwrite the DispatcherServletAutoConfiguration beans. All the rest of the magic works, multpart, Jackson etc. You can configure the Servlet and Jackson/Multipart etc. for each child-context separately and inject bean of the parent context.
package test;
import static org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME;
import static org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
#Configuration
#EnableAutoConfiguration(exclude = {
Application.Context1.class,
Application.Context2.class
})
public class Application extends WebMvcConfigurerAdapter {
#Bean
public TestBean testBean() {
return new TestBean();
}
public static void main(String[] args) {
final SpringApplicationBuilder builder = new SpringApplicationBuilder().parent(Application.class);
builder.child(Context1.class).run();
builder.child(Context2.class).run();
}
public static class TestBean {
}
#Configuration
#EnableAutoConfiguration(exclude = {Application.class, Context2.class})
#PropertySource("classpath:context1.properties")
public static class Context1 {
#Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
// custom config here
return dispatcherServlet;
}
#Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/test1");
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
// custom config here
return registration;
}
#Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(TestBean testBean) {
System.out.println(testBean);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
// custom config here
return builder;
}
}
#Configuration
#EnableAutoConfiguration(exclude = {Application.class, Context1.class})
#PropertySource("classpath:context2.properties")
public static class Context2 {
#Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
// custom config here
return dispatcherServlet;
}
#Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/test2");
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
// custom config here
return registration;
}
#Bean
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(TestBean testBean) {
System.out.println(testBean);
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
// custom config here
return builder;
}
}
}
The context1/2.properties files currently only contain a server.port=8080/8081 but you can set all the other spring properties for the child contexts there.
In Spring-boot ypu can use different profiles (like dev and test).
Start application with
-Dspring.profiles.active=dev
or -Dspring.profiles.active=test
and use different properties files named application-dev.properties or application-test.properties inside your properties directory.
That could do the problem.

How to customize the Jackson serializer for Spring SQS

How do you customize the Jackson JSON serializer for SQS? I've googled around, but so far everything I've found is related to the Spring web stuff, and there doesn't seem to be any way to get a hold of a reference to the serializer that Spring SQS uses, so that I can add my custom types (Java 8 Date stuff)
I just had a look at the source code of spring-cloud-aws to see how the Jackson object mapper is being instantiated, see here: QueueMessagingTemplate.java.
It turns out that it has a constructor that takes in a MessageConverter, so you could do this:
#Configuration
public class SpringAwsMessagingConfig {
#Bean
public QueueMessagingTemplate myMessagingTemplate(AmazonSQS amazonSqs, ResourceIdResolver resolver) {
ObjectMapper mapper = new ObjectMapper();
// configure the Jackson mapper as needed
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setSerializedPayloadClass(String.class);
converter.setObjectMapper(mapper);
return new QueueMessagingTemplate(amazonSqs, resolver, converter);
}
}

Resources