Home » Support » Forum » How to enhance the JPA/Spring/MVC to implement RESTfull for CURL client?

How to enhance the JPA/Spring/MVC to implement RESTfull for CURL client?

#1

Here is my attempt to enhance the JPA/MVC/eclipse tutorial to implement a RESTful service. Why does this not work with this curl command? I get 404. Can someone help me correct this? Do I need to change my web.xml servlet mapping in web.xml? I've been experimenting with and nothing seems to work.

Thanks

Siegfried

Here is my web.xml fragment:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <!-- <url-pattern>*.html</url-pattern> -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

Here is the curl command that causes a 404

curl -i -H "Accept: application/json" http://localhost:8080/Guestbook/user/siegfried -X GET

The logs say

Oct 11, 2013 11:21:28 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Guestbook/user/siegfried] in DispatcherServlet with name 'spring'

Here is my controller.

@Controller
@RequestMapping("/api")
public class APIController{
    @Autowired
    private GuestDao guestDao;

@RequestMapping(value = "/user/{username}", method = org.springframework.web.bind.annotation.RequestMethod.GET,
   headers = {"Accept=text/xml, application/json"})
public @org.springframework.web.bind.annotation.ResponseBody
Guest getGuest(@org.springframework.web.bind.annotation.PathVariable String username) {
  return guestDao.getAllGuests().get(0);
}
 

}

#2

Take a look at this part of your code:

@Controller
@RequestMapping("/api")
public class APIController{

Try http://localhost:8080/Guestbook/api/user/siegfried

#3

I had tried that previously and failed set a break point and failed to notice that I was getting a different error: 406.

So I just took your suggestion (again) and noticed I do hit my break point! Hurray! 

I think the problem is that it does not know how to convert the guest object to json. How do I fix that? Here is the error from curl:

 

curl -i -H "Accept: application/json" http://localhost:8080/Guestbook/api/user/siegfried -X GET
HTTP/1.1 406 Not Acceptable
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1067
Date: Sat, 12 Oct 2013 17:00:24 GMT

<html>
  <head>
    <title>Apache Tomcat/7.0.35 - Error report</title>
</head>
  <body>
    <h1>HTTP Status 406 - </h1>
    <HR size="1" noshade="noshade"/>
    <p>
      <b>type</b> Status report</p>
    <p>
      <b>message</b> <u>
      </u>
    </p>
    <p>
      <b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u>
    </p>
    <HR size="1" noshade="noshade"/>
    <h3>Apache Tomcat/7.0.35</h3>
  </body>
</html>

I'm confused! there was no new entry on the eclipse console log. This was left over from last night when I omitted the "api":

Oct 11, 2013 11:34:34 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Guestbook/user/siegfried] in DispatcherServlet with name 'spring'

Hmmm.... 

Well this is progress (I've been fighting with this a long time!).  So how do I get this to produce JSON code?

Thank you very much!

Siegfried

 

#4

I don't use Spring, but pure Java EE 6/7, so I can't help you further on.

There are many resources on internet that could help you. Start by searching your problem on http://stackoverflow.com/


Post Reply

To post a reply and/or subscribe to update notifications - please login