java 如何使用@RequestMapping 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3332150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:21:13  来源:igfitidea点击:

How to use @RequestMapping headers?

javaspringspring-mvc

提问by EdwardLau

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

我正在学习 springmvc。当我使用@RequestMapping(value="/helloWorld", headers = "content-type=text/*")并连接到 时http://localhost:8080/SpringMVC_10100/helloWorld,控制台输出以下内容:

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

警告 org.springframework.web.servlet.PageNotFound - 找不到与 servlet 请求匹配的处理程序方法:路径'/helloWorld'、方法'GET'、参数 map[[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 is

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?

为什么?

采纳答案by naikus

Its most likely the case that /helloworld is not inside the path configured for your dispatcher servlet

最有可能的情况是 /helloworld 不在为您的调度程序 servlet 配置的路径内

e.g. If i have a servlet configured like so:

例如,如果我有一个像这样配置的 servlet:

  <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 so:

我有一个这样配置的控制器:

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

Then the request in browsder would be:

然后浏览器中的请求将是:

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

Edit: Also if you have content-type header narrowing on your handler, make sure that content-type haeder is sent in your request.

编辑: 此外,如果您的处理程序有内容类型标头缩小,请确保在您的请求中发送内容类型标题。

回答by ravi

In the below annotation remove the headers:

在下面的注释中删除标题:

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

to:

到:

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

or to:

或者:

@RequestMapping(value="/helloWorld")

should make it work.

应该让它工作。