lundi 2 mars 2015

Spring JSON Response issue

I want to create some Spring based REST service sample. I want to get JSON based on Foo object, but when I try to send request using curl, it shows 406 error: "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.". Here is my code: RestControl.java



package hello;

import org.springframework.stereotype.*;

import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/app")
public class RestControl {
@RequestMapping(value="/get",produces="application/json", method=RequestMethod.GET)
@ResponseBody
public Foo getFoo(){
Foo f = new Foo();
f.setId(new Long(1));
f.setName("lol");
return f;
}
}


Foo.java:



package hello;

public class Foo {
private Long id;
private String name;

public void setId(Long id){
this.id=id;
}

public void setName(String name){
this.name=name;
}

public Long getId(){
return this.id;
}

public String getName(){
return this.name;
}

}


web.xml:



<web-app id="WebApp_ID" version="2.4"
xmlns="http://ift.tt/qzwahU" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/qzwahU
http://ift.tt/16hRdKA">

<display-name>Spring Web MVC Application</display-name>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>


mvc-dispatcher-servlet.xml:



<beans xmlns="http://ift.tt/GArMu6"
xmlns:context="http://ift.tt/GArMu7"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="
http://ift.tt/GArMu6
http://ift.tt/1cMYE2s
http://ift.tt/GArMu7
http://ift.tt/QEDs1k
http://ift.tt/OGfeU2
http://ift.tt/KC395X
http://ift.tt/1bHqwjR
http://ift.tt/1bVJL9q">


<mvc:annotation-driven />

<bean id="RestControl"
class="hello.RestControl" />

<bean id="foo"
class="hello.Foo" />

</beans>

Aucun commentaire:

Enregistrer un commentaire