获取HTTP状态404 - Spring mvc

时间:2015-06-08 00:55:32

标签: java spring-mvc http-status-code-404

嗨我正在尝试访问一个终点时获得Http状态404,该终点应该返回一个映射到终点的视图。我的应用程序启动正常,因为我的健康检查终点工作正常。请在下面找到我的代码段: 我已经尝试过在Stackoverflow上找到的类似问题的所有可能的建议,但它们似乎都不起作用。

我的结束点看起来像这样 - http://localhost:8080/web/views/home,这应该返回home.jsp但是返回http状态404.

健康检查终点 - http://localhost:8080/web/admin/health-check

的web.xml

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<display-name>Spring Demo</display-name>

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

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-servlet.xml
    </param-value>
  </context-param>

弹簧servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<context:component-scan base-package="cms.web, cms.service, cms.data"/>


<mvc:annotation-driven/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

HomeController.java

package cms.web.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/views")
public class HomeController {

@RequestMapping(value="/home", method = RequestMethod.GET)
public String getHomePage(){
    return "home";
 }
}

健康检查控制员 -

package cms.web.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
public class HealthCheckController {

/**
 * Returns a "200 OK" response if the service is running.
 *
 * @return String "200 OK"
 */
@RequestMapping( "admin/health-check" )
public String healthCheck()
  {
    return "OK";
  }
}

我在WEB-INF / views / home.jsp中有home.jsp。

1 个答案:

答案 0 :(得分:0)

解决方案是将web.xml中的DispatcherServlet的<url-pattern>/*</url-pattern>更改为<url-pattern>/</url-pattern>。进行此更改后,您可能会开始面临静态css和js文件未加载的问题。解决方案是将它们放在具有任何名称的文件夹中,例如。在 WEB-INF 中“static”,然后在spring-servlet.xml中指定<mvc:resources mapping="/static/**" location="/static/" />

要在jsp中包含静态资源,请使用此参考<link rel="stylesheet" href="<c:url value="/static/css/bootstrap.css" />" />"