[英]Spring MVC - Getting constant 404 errors using Tomcat
我已經使用 tomcat 使用 Maven 為我的大學項目創建了一個 Spring MVC Web 應用程序。 但是,當我訪問 /user/info URL 以及所有其他 URL 時,我一直在努力解決不斷出現的 404 錯誤。 /user/info URL 應該映射到 ProfileService 類和 getUserInfo() 方法,但不幸的是它失敗了,我以 404 錯誤告終。
我試圖找出導致 404 錯誤的問題。 我試過這個 url: localhost:8080/user/info 將 web.xml url-mapping 更改為 "/*" 沒有幫助。
問題是否位於阻止 URL 映射的 web.xml 或 spring-context.xml 文件的錯誤配置中?
當我訪問 / URL 時,index.jsp 文件被映射並且工作正常。
提前致謝。
spring-context.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<annotation-driven />
<context:component-scan base-package="ie" />
</beans:beans>
web.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
ProfileService.java 文件:
package ie.services;
import ie.logic.Loghme;
import ie.services.responses.CreditInfo;
import ie.services.responses.StatusCode;
import ie.services.responses.UserInfo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
@RestController
public class ProfileService {
@RequestMapping(value = "/user/info", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public UserInfo getUserInfo() {
return new UserInfo(Loghme.getInstance().getLoginnedUser());
}
@RequestMapping(value = "/user/credit", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public CreditInfo getUserCredit(){
return new CreditInfo(Loghme.getInstance().getLoginnedUser().getCredit());
}
}
您的映射不太正確。 您有一個 @PathVariable 作為方法參數,但您沒有在路徑中指定。 所以它應該更像這樣(查看@RequestMapping 中的 value=)。
@RequestMapping(value = "/user/info/{s}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public UserInfo getUserInfo(@PathVariable String s) {
return new UserInfo(Loghme.getInstance().getLoginnedUser());
}
但是,這意味着您需要在發出請求時將其放在那里,例如 GET "/user/info/hello"。
但是,如果您不使用它,您可以簡單地從方法和字符串中刪除它,然后可以調用“/user/info”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.