gpt4 book ai didi

java - 错误 404 Spring-MVC

转载 作者:行者123 更新时间:2023-12-01 13:10:55 25 4
gpt4 key购买 nike

我正在尝试 spring-mvc 和 json。

我的项目架构是

src
--main
----java
----resource
----webapp
------WEB-INF
--------web.xml
--------jodoCmsDispatcher-servlet.xml

我有一个普通 Controller

public class CategoryController {   
@RequestMapping(value = "/kfc", method = RequestMethod.GET)
public @ResponseBody Shop addCategory(HttpServletResponse response, @PathVariable("parentid") String parentCategoryId, @PathVariable("newcategorytitle") String newcategorytitle )
{
Shop shop = new Shop();
shop.setName("Testing");
shop.setStaffName(new String[]{"11", "22"});
return shop;
}
}

web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>

<servlet>
<servlet-name>jodoCmsDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jodoCmsDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

jodoCmsDispatcher-servlet.xml

.
.
<context:component-scan base-package="com.jodo.cms.controllers" />
<mvc:annotation-driven />

我尝试过以下网址(每次都得到 404)

localhost - - [05/Apr/2014:11:43:41 +0530] "GET /jodocms/kfc/ HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:20 +0530] "GET /jodocms/kfc HTTP/1.1" 404 949
localhost - - [05/Apr/2014:11:44:24 +0530] "GET /jodocms/ HTTP/1.1" 404 949

项目已成功部署在tomcat中,如localhost:8080/manager所示

我正在尝试发送 json 数据并遵循“http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
我缺少什么?
谢谢

最佳答案

这并不是真正需要的

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB_INF/jodoCmsDispatcher-servlet.xml</param-value>
</context-param>

由于 DispatcherServlet 默认加载 jodoCmsDispatcher-servlet.xml,因此删除这些行,该配置仅用于添加另一个 applicationContext 文件

并且CategoryController必须标记有@Controller才能被扫描

同时考虑改变

<context:component-scan base-package="com.jodo.cms.controllers" />

<context:component-scan base-package="com.jodo.cms" />

尝试删除..

@PathVariable("parentid") String parentCategoryId,
@PathVariable("newcategorytitle") String newcategorytitle

这些导致了冲突,因为您的 URL 不正确,因此保留了该方法。

  @RequestMapping(value = "/kfc", method = RequestMethod.GET)
public @ResponseBody Shop addCategory(HttpServletResponse response)
{
Shop shop = new Shop();
shop.setName("Testing");
shop.setStaffName(new String[]{"11", "22"});
return shop;
}

关于java - 错误 404 Spring-MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com