我正在尝试使用Spring 3.0创建一个RESTful控制器.控制器用于门户应用程序的管理API.我想要执行的操作是:
> GET / api /门户列出所有门户网站
> POST / api /门户创建新门户
> GET / api / portals / {id}来检索现有门户
> PUT / api / portals / {id}更新现有门户
> DELETE / api / portal / {id}删除现有门户
在如下图所示对控制器进行注释之后,我发现列出所有门户的操作或创建新的门户不会被映射.
所以我的问题是:
>我是否正确注释了课程?
>我是否遵循了实施RESTful Web服务的正确约定?
>春天可能会有什么东西坏掉吗?
@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
在启动期间,我看到Spring注册了它们的终点:
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@141717f]
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@141717f]
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@141717f]
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@141717f]
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@141717f]
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@141717f]
但是当我尝试使用cURL调用我的API时
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals/
要么
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals
我收到以下错误:
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'
当我尝试创建时遇到同样的问题:
curl -F ...... --request POST http://localhost:8080/com.btmatthtews.minerva/api/portals
但是如果尝试对现有资源进行操作(检索,更新或删除),它就可以了.
更新:解决方案在@axtavt的评论中提供.我使用< url-pattern> / api / *< / url-pattern>在我的web.xml servlet映射中.需要将其更改为< url-pattern> /< / url-pattern>
最佳答案
仔细检查web.xml中的url-pattern并将其与curl参数进行比较.
原文链接:https://www.f2er.com/spring/432488.html我编写的Here is an example引导您完成整个Spring MVC过程.