Spring boot controller content negotiation

Smajl

I have a simple REST controller written in a Spring-boot application but I am not sure how to implement the content negotiation to make it return JSON or XML based on the Content-Type parameter in the request header. Could someone explain to me, what am I doing wrong?

Controller method:

@RequestMapping(value = "/message", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
  public Message getMessageXML(@RequestParam("text") String text) throws Exception {
    Message message = new Message();
    message.setDate(new Date());
    message.setName("Test");
    message.setAge(99);
    message.setMessage(text);

    return message;
  }

I always get JSON when calling this method (even if I specify the Content-Type to be application/xml or text/xml).

When I implement two methods each with different mapping and different content type, I am able to get XML from the xml one but it does not work if I specify two mediaTypes in a single method (like the provided example).

What I would like is to call the \message endpoint and receive

  • XML when the Content-Type of the GET request is set to application/xml
  • JSON when the Content-Type is application/json

Any help is appreciated.

EDIT: I updated my controller to accept all media types

@RequestMapping(value = "/message", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }, consumes = MediaType.ALL_VALUE)
  public Message getMessageXML(@RequestParam("text") String text) throws Exception {
    Message message = new Message();
    message.setDate(new Date());
    message.setName("Vladimir");
    message.setAge(35);
    message.setMessage(text);

    return message;
  }
abarisone

You can find some hints in the blog post @RequestMapping with Produces and Consumes at point 6.

Pay attention to the section about Content-Type and Accept headers:

@RequestMapping with Produces and Consumes: We can use header Content-Type and Accept to find out request contents and what is the mime message it wants in response. For clarity, @RequestMapping provides produces and consumes variables where we can specify the request content-type for which method will be invoked and the response content type. For example:

@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
    return "method6";
}

Above method can consume message only with Content-Type as text/html and is able to produce messages of type application/json and application/xml.

You can also try this different approach (using ResponseEntity object) that allows you to find out the incoming message type and produce the corresponding message (also exploiting the @ResponseBody annotation)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a simple way to solve content negotiation in Spring boot

From Dev

Spring MVC Content Negotiation throwing ClassCastException

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

How to disable content negotiation for Spring Actuators?

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

spring cloud config-server .properties content-negotiation failing

From Dev

Is there better approach to Accept Header based content negotiation in Spring API?

From Dev

How to add csv MimeType to spring content-negotiation?

From Dev

Content negotiation to return HTML

From Dev

PhalconPHP: content negotiation?

From Dev

Restlet Content Type Negotiation

From Dev

Content negotiation using WebSharper

From Dev

PhalconPHP: content negotiation?

From Dev

Spring Boot, Thymeleaf and @Controller

From Dev

Spring Boot Controller 404

From Dev

Spring Boot Controller not mapping

From Dev

Spring Boot @RestController and @Controller

From Dev

spring boot static content

From Dev

Jersey - content negotiation for error handling

From Dev

Controller for JSON and HTML with Spring Boot

From Dev

spring boot problems with aop and controller

From Dev

rest controller not working in spring boot

From Dev

Spring boot controller retrieve UserDetails

From Dev

Junit for Spring Boot Rest Controller

From Dev

spring boot problems with aop and controller

From Dev

Spring Boot REST Controller issues

From Dev

Spring boot runtime add controller?

From Dev

Spring boot override function in controller

From Dev

SupportedMediaTypes not used when doing content negotiation