I am trying to create a RESTful controller using Spring 3.0. The controller is intended for the management API for the portal application. The operations that I want to perform are as follows:
- GET / api / portals to display all portals
- POST / api / portals to create a new portal
- GET / api / portals / {id} to retrieve an existing portal
- PUT / api / portals / {id} to update an existing portal
- DELETE / api / portal / {id} to delete an existing portal
After annotating the controller, as shown below, I found that the operations of listing all portals or creating a new portal are not displayed.
So my questions are:
- Did I formulate the class correctly?
- Am I following the correct conventions for implementing a RESTful web service?
- Could something be broken in Spring?
The code output below shows how I annotated my class:
@Controller @RequestMapping("/api/portals") public final class PortalAPIController { private final static Logger LOGGER = LoggerFactory.getLogger(PortalAPIController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public String listPortals(final Model model) { PortalAPIController.LOGGER.debug("Portal API: listPortals()"); . . return "portals"; } @RequestMapping(value = "/", method = RequestMethod.POST) public String createPortal(@RequestBody final MultiValueMap<String, String> portalData, final Model model) { PortalAPIController.LOGGER.debug("Portal API: createPortal()"); . . return "portal"; } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String getPortal(@PathVariable("id") final String portalId, final Model model, final HttpServletResponse response) throws IOException { PortalAPIController.LOGGER.debug("Portal API: getPortal()"); . . return "portal"; } @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public String updatePortal(@PathVariable("id") final String portalId, @RequestBody final MultiValueMap<String, String> portalData, final Model model, final HttpServletResponse response) throws IOException { PortalAPIController.LOGGER.debug("Portal API: updatePortal()"); . . return "portal"; } @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) public String deletePortal(@PathVariable("id") final String portalId, final Model model, final HttpServletResponse response) throws IOException { PortalAPIController.LOGGER.debug("Portal API: deletePortal()"); . . return "portal"; } . . }
During startup, I see that Spring is the things it registered endpoints for:
2010-02-19 01:18:41,733 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f] 2010-02-19 01:18:41,734 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f] 2010-02-19 01:18:41,734 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}.*] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f] 2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}/] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f] 2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f] 2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals.*] onto handler [ com.btmatthews.mars.portal.web.controller.PortalAPIController@14 1717f]
But when I try to call my API using cURL
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals/
or
curl http:
I get the following errors:
2010-02-19 01:19:20,199 WARN [org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/com.btmatthews.minerva.portal/api/portals] in DispatcherServlet with name 'portal' 2010-02-19 01:19:32,360 WARN [org.springframework.web.servlet.PageNotFound] - No mapping found for HTTP request with URI [/com.btmatthews.minerva.portal/api/portals/] in DispatcherServlet with name 'portal'
I get the same problem when I try to create:
curl -F ...... --request POST http:
But if you try to work with an existing resource (retrieve, update or delete), it works fine.
Update: The solution was provided in a comment by @axtavt . I used <url-pattern> / api / * </ url-pattern> in my web.xml servlet. It should be changed to <url-pattern> / </ url-pattern>