简体   繁体   中英

Getting HTTP Status 404 - Spring mvc

Hi I am getting Http status 404 when I am trying to access an end point which should return me a view mapped with the end point.My application is starting fine as my health-check end point is working fine. Please find my code snippets below: I have already tried all possible suggestions on similar issues that I found on Stackoverflow but none of them seems to work.

My end point looks like this - http://localhost:8080/web/views/home , this should return the home.jsp but is returning http status 404.

Health check end point - 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>

spring-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";
 }
}

Health check controller -

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";
  }
}

I have home.jsp inside WEB-INF/views/home.jsp.

The solution is to change the <url-pattern>/*</url-pattern> of the DispatcherServlet inside web.xml to <url-pattern>/</url-pattern> . After making this change you might start facing issues with your static css and js files not getting loaded. The solution for that would be to put them inside folder with any name for example. "static" inside WEB-INF and then specify <mvc:resources mapping="/static/**" location="/static/" /> in the spring-servlet.xml.

To include the static resources in your jsp use this for reference <link rel="stylesheet" href="<c:url value="/static/css/bootstrap.css" />" />"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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