繁体   English   中英

Spring MVC - 使用 Tomcat 不断出现 404 错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM