Can a RESTful service return both JSON and XML for the same resource, depending on the request header?
Asked Answered
F

3

10

I have a simple RESTful method which currently returns JSON representation of an object.

My question is more of from the architectural perspective and not completely technical.

Should a RESTful service be designed in such a way that it returns both JSON and XML at the same time?

As far as I know this is a bad practice and separate resources should be defined for this. One resource should return JSON data and other one XML.

Am I thinking it correctly?

Frontwards answered 12/3, 2014 at 20:4 Comment(2)
you mean using double the bandwidth for the same content?Stuckup
Sorry for the confusion. But my question should have been "Can a RESTful service generated both XML and JSON response at the same time but just send one based on the client's 'Accept' header" ?Frontwards
D
30

The same resource may return either XML or JSON depending upon the request, but it shouldn't return both at the same time. You will know which one to return based upon the request, so there is no need to generate both -- just generate the one you'll be returning.

Here is how you might choose to decide which to return. Evaluate in order, stopping when you've determined the format to return:

  1. If an extension has been added to the resource (GET /user/1234.json or GET /user/1234.xml), use that as the requested format.
  2. If an Accept header is set, use that header's value as the requested format.
  3. If there is a request body (as in the case of a POST), and the Content-Type header specifies JSON or XML, use that.
  4. Use a default format if none of the above apply (generally use JSON as your default unless your customers are generally still using XML).
Diathesis answered 13/3, 2014 at 17:14 Comment(0)
E
2

No. The way you represent your resource should be defined by what your clients expect (there is a http-header to say what representations the client accept). This means your server should check what is the representation the current client accepts and send the response in this representation (or send a response that says he cannot represent the resource in that media-type)

Eckardt answered 12/3, 2014 at 20:23 Comment(2)
Which is precisely what JAX-RS implementations do when you annotate the same method with two types: @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }). So, there is nothing wrong with using the same resource to produce responses in two formats, just know that this will not return a response with "XML and JSON" results simultaneously, it will choose one, the other, or neither according to the http Accept header. Check out the RFC for more info.Excite
Cool. So based on the "Accept" header which I assume is defined in the client's browser the response (either Json or Xml) can be dynamically selected by the Restful service at run time and sent back to the client. Correct?Frontwards
D
0

Step 1: Add below dependency under dependencies section in pom.xml

enter image description here

Step 2: Go to your controller class (where mention @RestController) and add below :

For get method :
@GetMapping(path = "/getNews",produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public getmethod
{
//your logic
}

same like for other method POST,PUT...
For post method :
@PostMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE} )
public postmethod
{
//your logic
}

Step 3: OUTPUT in POSTMAN
In header section add Key and value
For XML output add -> Key as Accept and Value as application/xml as below enter image description here For JSON output add -> Key as Accept and Value as application/json as below enter image description here

Doak answered 28/4, 2023 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.