Question

I saw a couple other questions of this nature but they didn't quite fit the bill.

I am currently trying to return JSON from two different controllers. In one of my controllers, it works with no problem, but when I hit my second endpoint, I get a 406 error and I can't figure out what's wrong. Here's the controller action:

@RequestMapping("/v1/companies/name/{companyName}")
public @ResponseBody ResultsList<CompanyResult> getCompaniesByName(@PathVariable("companyName") String companyName) {

    if (StringUtils.isEmpty(companyName)) {
        throw new HttpMessageNotReadableException("blah");
    }

    ResultsList<CompanyResult> results = companySearchService.getCompaniesByName(companyName);

    return results;
}

Here's the model I'm attempting to return:

public class ResultsList<T> {
    @XmlElement(name = "results")
    private ArrayList<T> results;

    public int resultCount() {
        return this.results.size();
    }

    public void addResult(T result) {
        this.results.add(result);
    }

    public ResultsList() {
        this.results = new ArrayList<T>();
    }
}

As you can see from the controller code, my ResultsList is made up of CompanyResult objects:

public class CompanyResult implements ICompany {
    @XmlElement(name = "companyName")
    private String companyName;

    @XmlElement(name = "symbol")
    private String symbol;

    @XmlElement(name = "city")
    private String city;

    @XmlElement(name = "state")
    private String state;
......

So my controller returns a @RequestBody ResultsList, which is annotated with @XmlElement, and the objects contained in the list are also annotated with @XmlElement. This is what I had to do to get the other scenario to work. But this one fails. Can anyone tell me why?

Was it helpful?

Solution 2

public class ResultsList<T> {
    private List<T> mResults;

    @JsonGetter("results")
    public final List<T> getResults() {
        return mResults;
    }

}

OTHER TIPS

According to the list of HTTP response codes 406 (Not Acceptable) means

The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

Therefore you probably have not set the Accept header to allow XML to be acceptable. Typically, JavaScript expects JSON by default. Therefore, to set the Accept header use something like the following:

function getCompany() {
 var client = new XMLHttpRequest();
 client.open("GET", "/v1/companies/name/exampleCompany");
 client.setRequestHeader("Accept", "application/xml");
 client.send();
}

Alternatively, you could set up your web service to produce application/json instead.

Make sure <mvc:annotation-driven> is in the Spring MVC context. This error occurres when Spring MVC does not recognize @ResponseBody (does not have registered HttpMessageConverter(s)) and therefore tries to convert the result object to the old fashion ModelAndView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top