Spring JSON request getting 406 (not Acceptable)

Asked
Active3 hr before
Viewed126 times

7 Answers

gettingspringrequest
90%

The server is not able to handle your request because the HTTP header “Accept” does not match with any of the content types it can handle.,You may get a HTTP 406 Not Acceptable error while trying to return Java objects from a REST controller.,In fact, the server might be unable to deliver the response in whatever content-type, some piece of configuration is missing in your project.,Just redeploy your webapp and you’ll be able to get your Java POJO as a JSON object.

@RequestMapping(value = "/dogs", method = RequestMethod.GET)
@ResponseBody
public List getDogs() {
   return this.dogRepository.getAllDogs();
}
load more v
88%

406 Spring MVC Json, not acceptable according to the request "accept" headers, Stack Overflow Public questions & answers ,Mapping restful ajax requests to spring, GitLab launches Collective on Stack Overflow

I had same issue, with Latest Spring 4.1.1 onwards you need to add following jars to pom.xml.

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-core</artifactId>
   <version>2.4.1</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.4.1.1</version>
</dependency>

also make sure you have following jar:

<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-core-asl</artifactId>
   <version>1.9.13</version>
</dependency>

<dependency>
   <groupId>org.codehaus.jackson</groupId>
   <artifactId>jackson-mapper-asl</artifactId>
   <version>1.9.13</version>
</dependency>
load more v
72%

For example, here is a simple RewriteRule that matches all incoming requests to https://airbrake.io/users/json that do not contain an Accept: application/json request header. The result is a redirection and 406 Not Acceptable response error code:,Since the 406 Not Acceptable is a client error response code, it’s best to start by troubleshooting any potential client-side issues that could be causing this error. Here are a handful of tips to try on the browser or device that is giving you problems.,The 406 Not Acceptable is an HTTP response status code indicating that the client has requested a response using Accept- headers that the server is unable to fulfill. This is typically a result of the user agent (i.e. browser) specifying an acceptable character set (via Accept-Charset), language (via Accept-Language), and so forth that should be responded with, and the server being unable to provide such a response.,As discussed in the introduction, a 406 Not Acceptable indicates that the user agent (the web browser, in most cases) has requested a valid resource, however the request included a special Accept- header that indicates to the server a valid response can only contain certain types of information. Here are a few examples of such scenarios:

For example, here is a simple RewriteRule that matches all incoming requests to https://airbrake.io/users/json that do not contain an Accept: application/json request header. The result is a redirection and 406 Not Acceptable response error code:

RewriteEngine on
RewriteCond % {
   REQUEST_URI
} ^ /users/json / ?.*$
RewriteCond % {
   HTTP_ACCEPT
}!application / json
RewriteRule ^ (.*) $ https: //airbrake.io/users/json$1 [R=406,L]
load more v
65%

below are some steps that need to be changed in configuration file so we can get rid of “Response Error 406 :-characteristics not acceptable according to the request “accept” headers ()”,This is the  very common mistake that typically happened during the configuration Spring + JSON. Recently we faced such issues with Spring 4 + Hibernate configuration so by digging some blogs and own effort make this issues fixed.,Thanks a lot PAL for this excellent post! I wasted a LOT of time to solve that terrible 406 error! Your post helped me to find right way to create rest-service. I’m very grateful to you for this info!,i was looking for to set my project for many hours , but i can´t fix the problen until raise here .. thanks a lot …!!!

Get rid of this issues by adding **@EnableWebMvc** in Controller class.

@Controller
@RequestMapping("/API/course")
@EnableWebMvc
public class CourseController {
   @Autowired
   private com.item.DAO.CourseRepository courseRepository;

   @ResponseStatus(value = HttpStatus.OK)
   @RequestMapping(method = RequestMethod.GET)
   public @ResponseBody List & lt;
   Course & gt;
   getListOfCourse() {
      List & lt;
      Course & gt;
      courses = courseRepository.getListOfCourse();
      return courses;
   }
load more v
75%

Proactive content negotiation headers include:, The HyperText Transfer Protocol (HTTP) 406 Not Acceptable client error response code indicates that the server cannot produce a response matching the list of acceptable values defined in the request's proactive content negotiation headers, and that the server is unwilling to supply a default representation. ,© 2005-2021 Mozilla and individual contributors. Content is available under these licenses.,BCD tables only load in the browser

406 Not Acceptable
40%

I keep on getting the following error: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()., 406 spring MVC JSON, accept header on request is not acceptable ,After so many attempts and variations of my code depending on advice found on various forums, my code still doesn't work., java - Spring MVC 406 is not acceptable, not JSON response

app-config.xml

    <context:component-scan base-package="org.ajaxjavadojo" />

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />
load more v
22%

The problem is when an exception is thrown and caught in ControllerAdvice, if I had the produces with MediaType.APPLICATION_JSON_VALUE stand first, it will work fine, a JSON Object will be returned.,However, MediaType.APPLICATION_OCTET_STREAM_VALUE stand first, 406 Not Acceptable status will be returned,and still throw an error when a JSON is returned.,In your case, both MediaType.APPLICATION_OCTET_STREAM_VALUE and MediaType.APPLICATION_JSON_VALUE are equally concrete. So the first one that matches is used.

@RequestMapping(value = "/download", method = RequestMethod.GET, produces = {
   MediaType.APPLICATION_JSON_VALUE,
   MediaType.APPLICATION_OCTET_STREAM_VALUE
})
load more v

Other "getting-spring" queries related to "Spring JSON request getting 406 (not Acceptable)"