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!

Steven G
  • 21
  • 4
  • 1
    remove headers attribute. try consumes=MediaType.APPLICATION_JSON_VALUE – Barath Aug 03 '17 at 15:51
  • See your comment, gives a 415 Http error – Steven G Aug 03 '17 at 15:59
  • check the endpoint , first try without setting the consumes or producs attribute as it is a simple get. – Barath Aug 03 '17 at 16:00
  • why in your HTTP header there is a path like `rest.htm` ?? although you are binding to **/rest** – AntJavaDev Aug 03 '17 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 :/ – Steven G Aug 03 '17 at 16:13
  • damn, then bro, thats why you cant hit that controller. Please, either change the dispatcher's mapping to smth like `/*` otherwise change the restController's mapping to `/rest.htm` – AntJavaDev Aug 03 '17 at 16:14
  • My other controller's first lines are these: @Controller @RequestMapping(value = "/product"). And this worked since day 1. I'll try to change it now. Edit: I accessed the controller by going to Schoolproject/product.htm btw – Steven G Aug 03 '17 at 16:18
  • yeap because your other controllers might be serving actual `.htm` files ??? and not simple json? – AntJavaDev Aug 03 '17 at 16:19
  • They are serving JSP pages. I tried changing /rest -> /rest.htm, didn't change a thing.. Edit: It has nothing to do with the fact I have two seperate controllers in different classes (same package ofcourse), right? – Steven G Aug 03 '17 at 16:20
  • no thats not a problem as long as they are getting wired up properly. So you actually changed the restController's mapping to `/rest.htm` and from your browser , you still got a 406 ? – AntJavaDev Aug 03 '17 at 16:26
  • Double checked, yes and still gives a 406. – Steven G Aug 03 '17 at 16:28
  • your actual mapping at the dispatcher is exactly : `.htm`? ill try to replicate the issue...... – AntJavaDev Aug 03 '17 at 16:30
  • *.htm Is the pattern. If you like I could zip the whole project and send it to you personally? (Think this would save time, no?) – Steven G Aug 03 '17 at 16:35
  • well indeed i replicated it successfully(got the same awkward 406 !!!), it only works if i remove the bad conf for `*.htm` from dispatcher. I noticed in spring logs that it tries to forward the request to a default ViewResolver(!!), although the proper controller has already responded(!!). Anyways , try to configure a new `Servlet` especially for handling the REST service part... If you need any bean to be shared to both servlet configs , then you ll have to configure an applicationContext. – AntJavaDev Aug 03 '17 at 17:04
  • Try to use `produces = "application/json"` instead of `headers = "Accept=application/json"` – GarRudo Aug 03 '17 at 17:09
  • @GarRudo , read the already posted answers please. – AntJavaDev Aug 03 '17 at 17:10
  • @StevenG , also another strange thing , is that if i put the mapping as `rest.jsp` or `rest.asd` it works like charm. The only suffix that does not work is `.htm`. quite strange and cant find anything related – AntJavaDev Aug 03 '17 at 17:12
  • @AntJavaDev Yes, you are right. StevenG, why don't you just try to use Spring Boot instead of this confusing Spring MVC. It's 2017 after all. – GarRudo Aug 03 '17 at 17:15
  • Another lesson we had is that we can to split web & domain into two different projects (and thus create a copy which consists of 2 projects, this time config in Java). Even though it doens't say I have to do this in the assignment, I'll try this and see if this works.Edit @GarRudo School says i have to... – Steven G Aug 03 '17 at 17:17
  • I just did what i said, and it worked. I don't know why it didnt work with both controllers in the same project, but I'm glad it works now. Appreciate the effort @AntJavaDev – Steven G Aug 03 '17 at 18:47
  • hmm , still isnt quite clear , i tried another thing , again mapped in `/rest.htm` , but this time i was converting the list and returning the JSON String myself and it was working properly. Feels like there is an issue with the predefined jackson library which in my case was coming from spring boot – AntJavaDev Aug 03 '17 at 19:14

3 Answers3

1

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

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public List<Product> getProducts() {
    return service.getAllProducts();
}
}
Plog
  • 9,164
  • 5
  • 41
  • 66
  • Doens't change the error. Still receiving HTTP 406. – Steven G Aug 03 '17 at 15:56
  • The thing is, people in guides/YT don't even use more annotations like produces/etc... It just works with the 'GET' method. – Steven G Aug 03 '17 at 16:05
  • @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 – Plog Aug 03 '17 at 16:08
  • Without .htm i get a 404 not found, and Chrome gives exactly the same response as postman since I'm only using GET. – Steven G Aug 03 '17 at 16:11
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

Barath
  • 5,093
  • 1
  • 17
  • 42
  • 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. – Steven G Aug 03 '17 at 15:58
  • why the request is going like this GET /SchoolProject/rest.htm HTTP/1.1 ? something wrong with the mapping – Barath Aug 03 '17 at 15:58
  • 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. – Steven G Aug 03 '17 at 16:09
  • have you tried removing consumes and produces ? try with ARC or postman by hitting the endpoint directly or do a curl – Barath Aug 03 '17 at 16:15
  • 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..) – Steven G Aug 03 '17 at 16:17
  • its a simple GET endpoint . just hit http://localhost:8080/rest/ it should end up showing the response otherwise something wrong with your mapping – Barath Aug 03 '17 at 16:30
  • That gives a 404 because it's not in the schoolproject's url. I'm not sure what you suggest I change...? – Steven G Aug 03 '17 at 16:34
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‌​">
  • I did not have the last one, instead i had 'http://www.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. – Steven G Aug 03 '17 at 16:06
  • Adding these jars should help jackson-core-asl-1.7.4.jar jackson-mapper-asl-1.7.4.jar – user8210845 Aug 03 '17 at 16:18
  • Added 1.9.13 (most recent) of both of those jars to my pom.xml, rebuilded, didn't do anything. – Steven G Aug 03 '17 at 16:24
  • Your problem must be relatable to any of these. Have a look https://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable – user8210845 Aug 03 '17 at 16:29