Sto cercando di creare un controller RESTful utilizzando Spring 3.0. Il controller è per un'API di gestione per un'applicazione portale. Le operazioni che voglio eseguire sono:Come configurare un controller RESTful in Spring 3 con annotazioni?
- GET/api/portali per elencare tutti i portali
- POST/api/portali per creare un nuovo portale
- GET/api/portali/{id} per recuperare un portale
- PUT/api/portali/{id} esistente per aggiornare un portale
- DELETE/api/portale/{id} esistente per eliminare un portale
esistente Dopo annotare il contr come illustrato di seguito, trovo che le operazioni per elencare tutti i portali o creare un nuovo portale non vengano mappate.
Quindi le mie domande sono:
- Ho io annotato correttamente la classe?
- Sto seguendo le convenzioni corrette per l'implementazione di un servizio Web RESTful?
- Potrebbe esserci qualcosa di rotto in primavera?
L'estratto di codice seguente mostra come ho annotato la mia classe:
@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";
}
.
.
}
Durante l'avviamento Sto vedendo che le cose primavera ha registrato gli end-point:
2010-02-19 01:18:41,733 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/] onto handler [[email protected]1717f]
2010-02-19 01:18:41,734 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}] onto handler [[email protected]1717f]
2010-02-19 01:18:41,734 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}.*] onto handler [[email protected]1717f]
2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals/{id}/] onto handler [[email protected]1717f]
2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals] onto handler [[email protected]1717f]
2010-02-19 01:18:41,735 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - Mapped URL path [/api/portals.*] onto handler [[email protected]1717f]
Ma quando provo a invocare la mia API usando cURL
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals/
o
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals
Ottengo i seguenti errori:
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'
ottengo lo stesso problema quando provo a fare un creare:
curl -F ...... --request POST http://localhost:8080/com.btmatthtews.minerva/api/portals
Ma se provate a operare su una esistente risorsa (recuperare, aggiornare o eliminare) funziona bene.
Aggiornamento: La soluzione è stata fornita in un commento @axtavt. Stavo usando < url-pattern >/api/* </url-pattern > nella mia mappatura servlet web.xml.Aveva bisogno di essere cambiato a < url-pattern >/</url-pattern >
Si potrebbe voler rimuovere il valore = "/" nell'elenco, creare metodi. potrebbe confondere il resolver url. un'altra cosa che ho notato è un problema con i namespace. i metodi sono mappati a "com.btmatthews.mars" e ricevi degli errori per "com.btmatthews.minerva" – giladbu
Prendendo il valore = "/" non ha fatto alcuna differenza ma quando ho provato ho visto che lo stesso URL i mapping sono stati registrati, quindi lo rimuoverò comunque. Il nome del pacchetto per i componenti del portale è com.btmatthews.mars ... ma l'applicazione costruita su di esso è impacchettata in com.btmatthews.minerva .... La sua confusione ma non un problema. –
L'unica possibilità che posso vedere è "" di DispatcherServlet errato. Per questa configurazione dovrebbe essere '/ url-pattern>'. –
axtavt