How to use @RequestMapping headers?

I am learning springmvc. When I use @RequestMapping(value="/helloWorld", headers = "content-type=text/*")and connect to http://localhost:8080/SpringMVC_10100/helloWorld, the following is displayed in the console:

WARN org.springframework.web.servlet.PageNotFound - No matching handler method was found for the servlet request: path '/helloWorld', method 'GET', parametersmap[[empty]]

My code is:

@Controller
public class HelloWordController {
    private Logger logger = LoggerFactory.getLogger(HelloWordController.class);

    @RequestMapping(value="/helloWorld", headers = "content-type=text/*")
    public ModelAndView helloWorld() {
        logger.debug("jin ru le");
        logger.info("The helloWorld() method is use");
        ModelAndView view = new ModelAndView();
        view.setViewName("/helloworld");
        return view;
    }
}

web.xml

<servlet>
    <description>This is Spring MVC DispatcherServlet</description>
    <servlet-name>SpringMVC DispatchServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>SpringContext</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringMVC DispatchServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Why?

+3
source share
2 answers

This is most likely the case when / helloworld is not inside the path configured for your dispatcher servlet

eg. If I have a servlet configured like this:

  <servlet>
    <servlet-name>BMA</servlet-name>
    <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>BMA</servlet-name>
    <url-pattern>/bma/*</url-pattern>
  </servlet-mapping>

And I have a controller configured like this:

@RequestMapping(value = "/planner/plan/{planId}/delete", method = RequestMethod.GET)
public ModelAndView deletePlanConfirm(HttpServletRequest request,  
       @PathVariable("planId")   Long planId)   {}

Then the request in the browser will look like this:

http://localhost:8080/bma/planner/plan/1223/delete

: , , .

+1

:

@RequestMapping(value="/helloWorld", headers = "content-type=text/*")

@RequestMapping(value="/helloWorld",  method = RequestMethod.GET)

:

@RequestMapping(value="/helloWorld")

.

0

Source: https://habr.com/ru/post/1756498/


All Articles