【问题标题】:Spring MVC - Request method 'POST' not supportedSpring MVC - 不支持请求方法“POST”
【发布时间】:2015-09-08 22:08:27
【问题描述】:

我正在学习 Spring MVC(使用 Thymeleaf),同时将 JBoss Seam 网站移植到 Spring MVC。

我正在尝试用 Spring Controller 替换 HTTPServlet(doPost 到 /myservlet),代码如下:

@RequestMapping(value="/myservlet", method = RequestMethod.POST)
public String executeAction(HttpServletRequest request, HttpServletResponse response) throws IOException {

     StringBuilder buffer = new StringBuilder();
     BufferedReader reader = request.getReader();

     String line;
     while((line = reader.readLine()) != null) {
         buffer.append(line);
     }

     String payload = buffer.toString();
     System.out.println("payload: " + payload);
     return "/index";
}

此方法需要读取通过 HTTP Post 发送到此端点的 XML Payload (String)。

当外部客户端(.NET - 将在实时环境中使用)调用它时,我会收到以下日志消息:

[org.springframework.web.servlet.PageNotFound] (default task-3) Request method 'POST' not supported
[org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-3) Handler execution resulted in exception: Request method 'POST' not supported

我也尝试过将其作为 HTTPServlet,但遇到了同样的问题。有人可以告诉我我做错了什么吗?

web.xml 内容为:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
    </error-page>

  <!-- Send unauthorised request to the 404 page -->
  <error-page>
    <error-code>403</error-code>
    <location>/404.html</location> 
  </error-page>

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
        /WEB-INF/spring/appServlet/servlet-context.xml
      </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

  <!-- Spring Security -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

</web-app>

xml 负载:

<Jobs>
    <Job Action="Post">
        <AdvertiserName>Advertiser1</AdvertiserName>
        <AdvertiserType ValueID="15897">15897</AdvertiserType>
        <SenderReference>01111111</SenderReference>
        <DisplayReference>DISPLAYREF_635346301296069467_4445_Test89
        </DisplayReference>
        <Classification ValueID="6211">1002915</Classification>
        <SubClassification></SubClassification>
        <Position><![CDATA[CASE MANAGER]]></Position>
        <Description><![CDATA[ The Case Manager role is the vital link between all parties within the mortgage application process. ...]]></Description>
        <Country ValueID="246">United Kingdom</Country>
        <Location ValueID="12096">Yorkshire</Location>
        <Area ValueID="107646">Bradford</Area>
        <PostalCode>BD1 1EE</PostalCode>
        <ApplicationURL>http://removed.com/Application.aspx?uPjAaXJ9HmZ04+4i/bqmFAz
        </ApplicationURL>
        <Language ValueID="120036">2057</Language>
        <ContactName>Joe Bloggs</ContactName>
        <EmploymentType ValueID="2163">2163</EmploymentType>
        <StartDate></StartDate>
        <Duration></Duration>
        <WorkHours ValueID="2190">2190</WorkHours>
        <SalaryCurrency ValueID="1078">1007000</SalaryCurrency>
        <SalaryMinimum>16200.00</SalaryMinimum>
        <SalaryMaximum>16200.00</SalaryMaximum>
        <SalaryPeriod ValueID="2178">1007600</SalaryPeriod>
        <JobType>APPLICATION</JobType>
    </Job>
</Jobs>

【问题讨论】:

  • 您能提供以下详细信息吗? (1)要求您从客户那里打? (2) Web.xml
  • 谢谢 - 我已经在上面添加了 web.xml 和有效负载内容
  • 对于初学者,请尝试public String executeAction(@RequestBody String payload) { 而不是您正在使用的executeAction 方法
  • 谢谢 - 我已经更改了方法签名,如果我使用测试页面它可以工作,但是当客户端尝试发送请求时我仍然得到:[org.springframework.web.servlet.PageNotFound ](默认任务9)不支持请求方法'POST' [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver](默认任务9)处理程序执行导致异常:请求方法
  • 远程客户端的响应返回“(405) Method Not Allowed”

标签: spring-mvc request-mapping


【解决方案1】:

如果其他人遇到此问题,则问题与方法签名和 CSRF 有关。

我按照 geoand 的建议(感谢)通过更改方法签名以添加 @RequestBody String 有效负载解决了这个问题

通过禁用特定 URL (/myservlet) 的 CSRF,但在 spring 安全配置中使用以下内容为其他 URL 启用它:

<http auto-config="true" use-expressions="true" pattern="/myservlet" >
  <csrf disabled="true"/>
</http>

<http auto-config="true" use-expressions="true" >    
  <access-denied-handler error-page="/403" />
  <form-login login-page="/login.html" authentication-failure-url="/login-error.html" authentication-success-handler-ref="customAuthenticationSuccessHandler" />
  <logout logout-success-url="/index" />
  <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
  <intercept-url pattern="/advertiser/**" access="hasRole('ROLE_ADVERTISER')" />
  <csrf disabled="false"/>
</http>

感谢大家的回复/cmets。

卡兹

【讨论】:

    【解决方案2】:

    我删除了crsf

    .and().csrf().and().exceptionHandling().accessDeniedPage("/Access_Denied"); 
    

    我变了

    and().exceptionHandling().accessDeniedPage("/Access_Denied");//this Work
    

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 2020-08-12
      • 2017-02-25
      • 2020-07-24
      • 2023-03-17
      • 2014-02-11
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多