Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, March 19, 2016

Spring 4, JSON - HTTP Error 406 Not Acceptable

Spring 4, JSON - HTTP Error 406 Not Acceptable


I've created controller for fullCalendar eventSource, unfortunatelly something went wrong and every time I try to open this page I got HTTP ERROR 406.

My Controller:

@RequestMapping(value="/schedule", method = RequestMethod.GET)  public @ResponseBody Object getEventyJSON(Principal principal){      String name = principal.getName();      List eventy = userService.findOneWithEventy(name).getEventy();        List> events = new ArrayList>();        for (int i = 0; i < eventy.size(); i++) {          HashMap map = new HashMap();          map.put("title", eventy.get(i).getTitle());          map.put("start", eventy.get(i).getStart());          events.add(map);      }        return events;  }  

My POM.xml

      com.fasterxml.jackson.core      jackson-core      2.4.1            com.fasterxml.jackson.core      jackson-databind      2.4.1.1    

Of course I've tried to change dependency to codehaus instead of fasterxml and it's not working. Also I've tried to add some headers to method.

When i change controller to this:

@RequestMapping(value="/schedule", method = RequestMethod.GET)      public @ResponseBody Object getEventyJSON(Principal principal){          String name = principal.getName();          List eventy = userService.findOneWithEventy(name).getEventy();              return "schedule";      }  

It is working and when I open that page it print schedule so it is correct. What am I missing?

Answer by Alan Barrows for Spring 4, JSON - HTTP Error 406 Not Acceptable


In the call you are making to the resource you need to tell it what content type you are expecting

If you are using JQuery something like this would work

$.ajax({      type: "GET",      url: "/schedule",      contentType: "application/json",      dataType : "json",      success: function (d) {          // do something with the json      }  });  

Answer by FrAn for Spring 4, JSON - HTTP Error 406 Not Acceptable


406 happens when the server(your back end) cannot respond with the Accept-Type HTTP header specified in the request. So basically your server is returning different response type that is in the Client requests Accept-Header.

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

check here

Answer by Brian Clozel for Spring 4, JSON - HTTP Error 406 Not Acceptable


Try this:

@RequestMapping(value="/schedule", produces= "application/json")  public @ResponseBody List getEventy(Principal principal){  

Answer by Pierre for Spring 4,

JSON - HTTP Error 406 Not Acceptable

Try changing your method to return a String and, within your method, transform your list into a JSON String. For that, you could use google-gson but, given that we're playing with spring already and probably already have those dependencies in the project, you can use Jackson's ObjectMapper. like this:

    @RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")  public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {          // NOTE1: change the method to return a "String"          // NOTE2: add "throws JsonProcessingException" to the method (or catch it)      String name = principal.getName();      List eventy = userService.findOneWithEventy(name).getEventy();      List> events = new ArrayList>();        for (int i = 0; i < eventy.size(); i++) {          HashMap map = new HashMap();          map.put("title", eventy.get(i).getTitle());          map.put("start", eventy.get(i).getStart());          events.add(map);      }      //return events;      ObjectMapper mapper = new ObjectMapper();      return mapper.writeValueAsString(events);  }  

Also add these 2 imports:

import com.fasterxml.jackson.core.JsonProcessingException;  import com.fasterxml.jackson.databind.ObjectMapper;  

I'm pretty sure the above will fix it for you. But there is one more thing to try if the above doesn't fix your 406 error response, add this to the @RequestMapping:

headers="Accept=*/*",produces = "application/json"  

ex:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")  

Answer by sagar for Spring 4, JSON - HTTP Error 406 Not Acceptable


Try changing your method to return a String and, within your method, transform your list into a JSON String. For that, you could use google-gson but, given that we're playing with spring already and probably already have those dependencies in the project, you can use Jackson's ObjectMapper. like this:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/json")  public @ResponseBody String getEventyJSON(Principal principal) throws JsonProcessingException {      // NOTE1: change the method to return a "String"      // NOTE2: add "throws JsonProcessingException" to the method (or catch it)      String name = principal.getName();      List eventy = userService.findOneWithEventy(name).getEventy();      List> events = new ArrayList>();        for (int i = 0; i < eventy.size(); i++) {          HashMap map = new HashMap();          map.put("title", eventy.get(i).getTitle());          map.put("start", eventy.get(i).getStart());          events.add(map);      }      //return events;      ObjectMapper mapper = new ObjectMapper();      return mapper.writeValueAsString(events);  }  

Also add these 2 imports:

import com.fasterxml.jackson.core.JsonProcessingException;  import com.fasterxml.jackson.databind.ObjectMapper;  

I'm pretty sure the above will fix it for you. But there is one more thing to try if the above doesn't fix your 406 error response, add this to the @RequestMapping:

headers="Accept=*/*",produces = "application/json"  

Ex:

@RequestMapping(value="/schedule", method = RequestMethod.GET,headers="Accept=*/*",produces = "application/js  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.