question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content negotiation for XML response

See original GitHub issue

Given the example controller method shouldn’t XML be returned for the Firefox default Accept header "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"?

@RequestMapping(value = "/test", method = RequestMethod.GET)
    public ResponseEntity<String[]> test(HttpServletRequest request) {      
        String[] result = {"value 1", "value 2", "value 3"};        
        return new ResponseEntity<String[]>(result, HttpStatus.OK);             
    }

The following URLs return a content type of “application/json;charset=UTF-8”:

But the URL http://localhost:9000/test.xml results in a “406 Not Acceptable”.

Setting the Accept header, using modify headers plugin, to only accept “application/xhtml+xml” for the URL http://localhost:9000/test also results in the same response.

Can someone explain what is happening?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
berinlecommented, Sep 27, 2014

Ok. I was finally able to figure this out by extending ArrayList class and annotating it accordingly. Below is a solution for any others looking:

@RequestMapping("/foo2")
Foos getFoos() {
    Foos f = new Foos();
    f.add(new Foo("bar"));
    f.add(new Foo("baz"));
    f.add(new Foo("bat"));
    return f;   
}

With Foos class defined as…

@XmlRootElement
class Foos extends ArrayList<Foo> {
    public Foos() { }

   @XmlElement(name = "foo")
   public List<Foo> getFoos() {
       return this;
   }
}

Thanks Dave!

1reaction
dekicommented, Jan 19, 2016

Of course the solution works, but why is it needed? See comparison here: http://blog.codeleak.pl/2015/04/jax-rs-2x-vs-spring-mvc-returning-xml.html

Is there any better solution without wrapper?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot and Content Negotiation - XML and JSON ...
This guide will help you implement Content Negotiation for a REST API/Service with Spring Boot. Content Negotiation helps the Consumer and ...
Read more >
Implement Content Negotiation using Spring Boot - JavaInUse
In this post we implement a simple Spring Boot example to implement Content Negotiation for returning XML or JSON depending upon the URL...
Read more >
Content Negotiation Implementing Support for XML - Javatpoint
Content negotiation is the process of selecting the best representation for a given response when there are multiple representations available.
Read more >
Content Negotiation using Spring MVC
When making a request via HTTP it is possible to specify what type of response you would like by setting the Accept header...
Read more >
Spring MVC Content Negotiation - Baeldung
A guide to configuring content negotiation in a Spring MVC application and on enabling and disabling the various available strategies.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found