Bri*_*ews 6 java rest spring spring-mvc
我正在尝试使用Spring 3.0 创建RESTful控制器.控制器用于门户应用程序的管理API.我想要执行的操作是:
在如下图所示对控制器进行注释之后,我发现列出所有门户的操作或创建新的门户不会被映射.
所以我的问题是:
下面的代码提取显示了我如何注释我的类:
@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";
}
.
.
}
Run Code Online (Sandbox Code Playgroud)
在启动期间,我看到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]
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用cURL调用我的API时
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals/
Run Code Online (Sandbox Code Playgroud)
要么
curl http://localhost:8080/com.btmatthews.minerva.portal/api/portals
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
当我尝试创建时遇到同样的问题:
curl -F ...... --request POST http://localhost:8080/com.btmatthtews.minerva/api/portals
Run Code Online (Sandbox Code Playgroud)
但是如果尝试对现有资源进行操作(检索,更新或删除),它就可以了.
更新:该解决方案由@axtavt发表评论.我在web.xml servlet映射中使用<url-pattern>/api/*</ url-pattern>.需要将其更改为<url-pattern>/</ url-pattern>
您在卷曲摘录中发布的 URL
http://localhost:8080/portal/api/portals
Run Code Online (Sandbox Code Playgroud)
与 Spring 警告中的 URL 不匹配
/com.btmatthews.minerva.portal/api/portals
Run Code Online (Sandbox Code Playgroud)
如果不知道你的 web 应用程序是如何设置的、它所在的上下文路径是什么、Spring 上下文是什么样子等等,就很难诊断,但这对我来说听起来像是一个很大的线索。
| 归档时间: |
|
| 查看次数: |
16954 次 |
| 最近记录: |