Steven G
Steven G

Reputation: 21

Spring REST 406 Http error

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!

Upvotes: 2

Views: 465

Answers (3)

user8210845
user8210845

Reputation: 41

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‌​">

Upvotes: 0

Barath
Barath

Reputation: 5283

@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

Upvotes: 0

Plog
Plog

Reputation: 9622

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

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

Upvotes: 1

Related Questions