2

Edit: FIXED! No need to reply

I have to create a project for school and I can't find the solution to this HTTP error that keeps coming up...

I'll try to make the code as short as possible without forgetting any.

I am using Spring MVC with XML config:

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
   etc..>

<context:component-scan base-package="ui.controller"/> 
<mvc:annotation-driven/>
</beans>

Pom.xml:

  <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.7.3</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.7.3</version>
    </dependency>

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

Rest Controller:

@RestController
@RequestMapping(value = "/rest")
public class ProductRESTController {

private final ProductService service;

public ProductRESTController(@Autowired ProductService service) {
    this.service = service;
}

@RequestMapping(method = RequestMethod.GET, headers = "Accept=application/json")
public List<Product> getProducts() {
    return service.getAllProducts();
}
}

We have to use Postman to check the functionality of our REST controller, so i'll post the header code aswell:

GET /SchoolProject/rest.htm HTTP/1.1
Host: localhost:8080
Accept: application/json
Cache-Control: no-cache
Postman-Token: 1543765c-b6c0-c82a-6c7d-6e4ce445fa16

I have tried multiple things, changed the code several times, but nothing works. I keep getting 406 HTTP error:

"The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."

Even though I do client & server side Application/Json...

Please help!

21
  • 1
    remove headers attribute. try consumes=MediaType.APPLICATION_JSON_VALUE Commented Aug 3, 2017 at 15:51
  • See your comment, gives a 415 Http error Commented Aug 3, 2017 at 15:59
  • check the endpoint , first try without setting the consumes or producs attribute as it is a simple get. Commented Aug 3, 2017 at 16:00
  • why in your HTTP header there is a path like rest.htm ?? although you are binding to /rest Commented Aug 3, 2017 at 16:05
  • In my Web.xml i use a servlet mapping to *.htm, and give it to the dispatcher, pretty sure this has nothing to do with the error :/ Commented Aug 3, 2017 at 16:13

3 Answers 3

1

You should use the 'produces' property on @RequestMappings:

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public List<Product> getProducts() {
    return service.getAllProducts();
}
}
Sign up to request clarification or add additional context in comments.

4 Comments

Doens't change the error. Still receiving HTTP 406.
The thing is, people in guides/YT don't even use more annotations like produces/etc... It just works with the 'GET' method.
@StevenG Yeah I think its usually default behavior to return things as json but in my experience when I've had similar issues with 406's this has solved it for me. Can you try just browse to the URL instead of using postman? And leave off the .htm I don't think that should be there
Without .htm i get a 404 not found, and Chrome gives exactly the same response as postman since I'm only using GET.
0
@RequestMapping(value="/getProducts",method = RequestMethod.GET,consumes=MediaType.APPLICATION_JSON_UTF8_VALUE,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<Product> getProducts() {
    return service.getAllProducts();
}

GET /getProducts

7 Comments

HTTP error 415: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
why the request is going like this GET /SchoolProject/rest.htm HTTP/1.1 ? something wrong with the mapping
I can assure you the mapping is right. I use another controller for non-Rest stuff and every single url works there. I even changed the url today from /SchoolProject/Product/rest.htm > /SchoolProject/rest.htm just to see this wasn't the problem.
have you tried removing consumes and produces ? try with ARC or postman by hitting the endpoint directly or do a curl
I have. And I have no idea what you mean by hitting the endpoint directly or curl. (you mean without the whole .htm part? Pretty sure I have to create my whole website with it..)
|
0

Is this what you have in your xml? Have a look.

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mv‌​c http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd‌​">

4 Comments

I did not have the last one, instead i had 'springframework.org/schema/mvc/spring-mvc.xsd'. I tried adding yours and it gave a lot of error when running the project.. Edit: I had all the other ones though.
Adding these jars should help jackson-core-asl-1.7.4.jar jackson-mapper-asl-1.7.4.jar
Added 1.9.13 (most recent) of both of those jars to my pom.xml, rebuilded, didn't do anything.
Your problem must be relatable to any of these. Have a look stackoverflow.com/questions/7462202/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.