How to configure a RESTful controller in Spring 3 with annotations?

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://localhost:8080/com.btmatthews.minerva.portal/api/portals 

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://localhost:8080/com.btmatthtews.minerva/api/portals 

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>

+6
java spring rest spring-mvc
source share
4 answers

Double check the url-pattern in web.xml and compare it with the curl argument.

Here is an example that I wrote that looks at the entire Spring MVC process.

+4
source share

The url you post in your curl breakaway

 http://localhost:8080/portal/api/portals 

does not match Spring warning url

 /com.btmatthews.minerva.portal/api/portals 

Not knowing how your webapp is configured, what context path it uses, what the Spring context looks like, etc., it's pretty hard to diagnose, but it sounds like a big key to me.

0
source share

You made some mistakes.

 @RequestMapping(value = "/", method = RequestMethod.GET) public String listPortals(final Model model) { } 

Here, the method should accept the Model parameter, but this is not passed. you have to write this method inside. or if you want to pass this, u should send it as a path variable or as a request parameter.

 @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String getPortal(@PathVariable("id") 

here is the correct syntax @PathVariable(value="id")

And if this does not work then try the following:

 @RequestMapping(value = "/SOMETHING", method = RequestMethod.GET) 
0
source share

In Spring 3, the break controller is nothing more than a regular controller (@Component), and the only difference is that the Restor controller returns JSON / XML instead of β€œViews”. So I think you are fine with annotations. However, I don’t see two missing things: 1- Each method must have a value of "produce" (for content matching): @RequestMapping (value = "/ {id}", method = RequestMethod.GET, produces = "application / json")

2- You need some type of ObjectMapper (for example: Marshaller and Unmarshaller) to be able to map objects to JSON / XML and vice versa. These two should be in your servlet configuration, and not in the recognizer configuration. And that should be so.

0
source share

All Articles