How Should I Make Spring Boot Rest API's Return Response in Client Requested Formate - spring-boot

I have Spring boot Rest APIS, but i want make those APIS Response as per the Client Requested Format.
For Instance :
Rest APIS Return Response By default In Json format. But Client Want to in XML formate or any other formate.
In the above situation how can i make my APIS dynamically Retunr the Response as per the Client requested format.
can any tell how can i do this.
Thanks in advance.

What you are talking about is Content Negotiation - Here's a Baeldung article that describes how you might use Jackson libraries to handle both XML and JSON data,
https://www.baeldung.com/spring-mvc-content-negotiation-json-xml
You need to devise a content negotiation strategy - very often, that is the use of the Accept header. Your API then has to respect this header and return the appropriate content, which might mean your API contracts having certain attributes to allow it to easily serialize to JSON/XML.
The article linked above links to a github repo that shows how this might be achieved - https://github.com/eugenp/tutorials/tree/master/spring-mvc-basics

Related

Download different content types with spring

I need to expose a POST endpoint where the user uploads an excel file and based on some validations, I either send back the file with some information added to it along with json response OR just send status code as 200 OK(no data).
I am trying to do this in spring boot. I tried following link:
https://javadigest.wordpress.com/2012/02/13/downloading-multiple-files-using-multipart-response/
This works but needs adding boundary manually. Is there any other way to do it so that I can send both the data ?
You should use #Produces as it is written here: https://docs.oracle.com/cd/E19776-01/820-4867/ghrpv/index.html
You can define the MIME-Type of your payload.

Spring Boot - Graphql - Access Headers from arbitrary resolver code

I'm using GraphQL (Spring Boot, Kotlin), and I have a specific issue with headers.
We need the client to send three pieces of information that we combine to retrieve internal sensitive data. That data is then used inside resolver codes.
Getting the headers from rest is as easy as #RequestHeader, but in GraphQL it has proved quite challenging. Is there a way I can save the headers to check for the three pieces of information I need?
I looked closely at spring security, but I can't figure out how to get a custom method to work in my method chain, much less save the headers in a way that I can access them from the domain layer.
I could always simply request the auth as a GraphQL type, but that puts a fairly high burden on the client and complicates the schema.
Any and all feedback is welcome.
GraphQL has a FetchDataEnvironment. You can use it by passing FetchDataEnvironment as the last variable in any resolver/query. Then, just call
environment.getContext<GraphQLEnvironentClass>().getHttpServletRequest.headers()
To anyone in the future - I'm sorry this code isn't exact, but your strong typing in your IDE will show you what I mean.

Spring MVC REST API: Invoke controller method programmatically given URL and JSON request body

I have a general REST API (developed using Spring MVC) that takes a list of API requests as its request body.
Each API request in the list has its own URL and request body.
In the implementation of this general REST API, I need to call the corresponding Spring controller method (in the same app) for each of these individual API requests (with their appropriate URL and request body).
(I will then merge all those individual API responses and return it in one big response from the general REST API).
I've been searching around, but I'm unclear how to programmatically call Spring to execute each individual API request. I would ideally like to get back the ResponseEntity from each call instead of the actual JSON response.
(More information:
On the same app server as the general API, I need to translate the URL and JSON request body for each individual API into the arguments to the controller method. I also need to take the URL and have Spring determine which controller method to invoke itself.)
Any help would be greatly appreciated.
Thanks,
Matt
Answer depend on whether the individual URLs that you are planning to invoke is with in the same server (Accessible without using network call) or not
If it is with in the same app server, spawn multiple threads and invoke the individual methods and join the response together and send it back
If it is not within the same app server, there are many Async Restclients are there besides spring's own webclient/restTemplate etc

How to specify Content-Type of all responses once in API Blueprint?

In order to satisfy Dredd, I have to write this for every response in my API Blueprint document:
+ Response 201 (application/json; charset=utf-8)
Is there a way to specify the media type ((application/json; charset=utf-8)) once, globally, for every response? This would have cleaned the document a bit.
As far as I know, this is not a supported scenario. I suggest you to file an issue on API Blueprint repository on GitHub to eventually get this feature request evaluated!

Forming a SOAP request message through WSDL

I'm very new to webservices. I'm trying to figure out how I can formulate a request message (and determine what the response message) would be based on the wsdl description that I have.
This is from a third party web service. The WSDL description that I have access to gives me a bunch of information like <types> <message> <operation> etc.
But in the examples that I've seen online, it's showing the request mesage within the "soap:envelope" tag.
What am I missing?
Eventually I'd like to be able to call this webservice using JQuery. But I can't even figure out how to formulate the request message let alone make an ajax call to it.
any help would be appreciated.
For these types of situations I would download soapUI, point it to your WSDL and use it to generate a few sample requests to get familiar with the endpoints, messages and the data model (XSD) for the service.
Armed with soapUI's sample requests it should be fairly easy to move this to jQuery's SOAP client (assuming of cause that the service is not humongous and requires you to transfer a big object graph as XML - in these cases you might want to check if your service vendor has a REST API as these are generally very easy to work with from jQuery).

Resources