java - 不支持请求方法 'HEAD'

标签 java spring-mvc tomcat

我用 apache tomcat 7.0.55 运行了一个新的 Intellij Idea 实例,我创建了一个新的简单项目,但我不断收到这个错误:

Feb 28, 2015 11:54:13 PM org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'HEAD' not supported

Controller :

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value="/",method = {RequestMethod.GET, RequestMethod.HEAD})
public class HelloController {
    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello world!");
        return "hello";
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springapp</groupId>
    <artifactId>untitled</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>untitled</name>

    <properties>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>untitled</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最佳答案

在类里面,

@RequestMapping(value="/",method = {RequestMethod.GET, RequestMethod.HEAD})
public class HelloController {

您声明您的 Controller 将处理根 URL /GETHEAD 请求。

但是在方法层面,

@RequestMapping(method = RequestMethod.GET)

您只接受GET 请求。 Spring 是对的,请求方法 'HEAD' 不支持

您应该在方法级别接受所有内容 (@RequestMapping(value = "") ),或者创建第二个方法来处理 HEAD 请求。

关于java - 不支持请求方法 'HEAD',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791528/

相关文章:

java - 如何使用 GCM 从 Java 服务器向 Android 应用发送通知?

tomcat - 当我使用 tomcat 插件时,Intellij 在哪里部署我的 war ?

spring - Spring boot 或 tomcat 中不同域的多个 ssl 证书

java - 使用 ServletContainer 作为 Servlet 提供静态资源

在 Windows/Linux 机器上部署和测试的 Java 工具(持续集成)

java - 重构代码的好方法

java - 为什么 Spring 在处理端点异常时将 RuntimeException 包装在 NestedServletException 中?

java - 抛出异常时重定向请求

apache - 在 Openshift 上使用 JBoss cartridge 配置 Apache HTTP 服务器

Java从具有多列的文本文件中读取输入