0

Im using spring to build a Restful API and when I access the method below:

// get the entity in DB by using id number
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
    public @ResponseBody
    User getEmployee(@PathVariable("id") String email) {
        User user=null;
        System.out.println(email);
        try {
            user = dataServices.getEntityById(email);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(user.getNickname());
        return user;
    }

with this URL: http://localhost:8080/RestCrud/user/richard_johnson@sina.com

it gets 406 error:

enter image description here

I make sure I have added the

<mvc:annotation-driven />

in my spring-config.xml. enter image description here

Im also sure I added those jackson dependencies in pom.xml

enter image description here

********************edit*************************************

enter image description here

********************edit again*******************************

as you can see I did not restrict headers in the @RequestMapping annotation, so I dont think its an issue related to the header restriction.

Also, my url patterns are like:

enter image description here enter image description here

http://localhost:8080/RestCrud/user/id

Ive tested the "list" http://localhost:8080/RestCrud/user/list

and it works, but the "id" path does not

Qing
  • 1,401
  • 2
  • 21
  • 41

2 Answers2

1

It seems like you have commented out a few jackson dependencies The error occured because your employee object couldnt be converted to a format accceptable by the browser. You definitley would have wanted to respond with json.

Spring 4 required the following jackson libs

  • jackson-core
  • jackson-core-sal
  • jackson-mapper-asl
  • jackson-databind

Updated:

Looking at your URL pattern , the *.com extension is pushing spring to perform a content negotiation rather than verifying the accept headers.

You can force spring not to do content negotiation based on path extension using

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>
jozzy
  • 2,863
  • 3
  • 15
  • 12
  • Hi Jozzy ive edited the post, even thought I added those dependencies, still not work – Qing Aug 13 '15 at 09:12
0

Look at this answer, in short make sure you are including the jackson-core and jackson-databind dependencies if you are using Spring 4

Community
  • 1
  • 1
dimitrisli
  • 20,895
  • 12
  • 59
  • 63
  • Hi dim ive edited the post, even thought I added those dependencies, still not work – Qing Aug 13 '15 at 09:12