Spring Boot 应用 Post 方法调用,405: Method Not Allowed

Posted

技术标签:

【中文标题】Spring Boot 应用 Post 方法调用,405: Method Not Allowed【英文标题】:Spring boot application Post method invocation, 405: Method Not Allowed 【发布时间】:2016-12-10 14:04:33 【问题描述】:

我正在使用 Spring Boot 编写 Rest Application。将我的服务代码公开为 REST 服务。

当我使用控制器和 Pojo 类的以下代码编写 Post 方法时,我能够在 GET 方法中公开我的服务,我得到 405: Method Not Allowed 错误。 不明白为什么?

我也提到了这个link. 和其他相关的,但我不知道是什么问题。

下面是我的控制器和带有jackson Json注释代码的Pojo。

当我使用Advanced REST client - Chrome Web Store - Google 调用并使用attached image 时,我遇到了错误。

在同一个类中,我有一些工作正常的 GET 方法。 错误 : 网址:http://localhost:8085/DBService/application/saveApplicationAnswer


"timestamp": 1470313096237
"status": 405
"error": "Method Not Allowed"
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException"
"message": "Request method 'POST' not supported"
"path": "/DBService/application/saveApplicationAnswer"

DBService 是我的上下文名称

正如我在application.properties 中设置的server.context-path=/DBService

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.drd.hotel.db.service.IApplicationDBService;
import com.drd.hotel.db.service.dto.application.CustomerDTO;
import com.drd.hotel.db.service.dto.application.ApplicationAnswerDTO;
import com.drd.hotel.db.service.dto.application.ApplicationQuestionsDTO;
import com.drd.hotel.db.service.dto.application.ApplicationRecommendationDTO;
import com.drd.hotel.db.service.util.ServicesConstants;

  @RestController
@RequestMapping("/application")
public class ApplicationDBController<T, I extends Serializable> 

    @Autowired
    private IApplicationDBService applicationDBService;



    @RequestMapping(value = "/saveApplicationAnswer", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public int saveApplicationAnswer(@ModelAttribute(ServicesConstants.SURVERY_ANSWER_FL) ApplicationAnswerDTO applicationAnswer) 

        LOG.info("ApplicationDBController fn saveApplicationAnswer BookingId   ",applicationAnswer.getBookingId(),  ServicesConstants.CUSTOMER_ID_FL, applicationAnswer.getCustomerId());
        return applicationDBService.saveapplicationAnswer(applicationAnswer);
    


我的 JSON:

"answerId":1,"applicationQuestionId":1,"recommendId":1,"bookingId":123001,"customerId":19501,"reasonForCancelation":"I dont konw ","feedbackText":"I dont know what is this too bad design","applicationDate":"2016-08-04","funnelPageName":"I dont know what is the use of this.","applicationReferenceSource":"I dont knwo what is this field for","languageId":1

我的 Pojo 用 JSON 注释:

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;




    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
    @JsonSerialize(include =Inclusion.NON_NULL)

    public class ApplicationAnswerDTO 

        private int answerId;
        private int applicationQuestionId;
        private int recommendId;
        private int bookingId;
        private int customerId;
        private String reasonForCancelation;
        private String feedbackText;
        private Date applicationDate;
        private String funnelPageName;
        private String applicationReferenceSource;
        private int languageId;

        public int getAnswerId() 
            return answerId;
        
        public void setAnswerId(int answerId) 
            this.answerId = answerId;
        
        public int getApplicationQuestionId() 
            return applicationQuestionId;
        
        public void setApplicationQuestionId(int applicationQuestionId) 
            this.applicationQuestionId = applicationQuestionId;
        
        public int getRecommendId() 
            return recommendId;
        
        public void setRecommendId(int recommendId) 
            this.recommendId = recommendId;
        
        public int getBookingId() 
            return bookingId;
        
        public void setBookingId(int bookingId) 
            this.bookingId = bookingId;
        
        public int getCustomerId() 
            return customerId;
        
        public void setCustomerId(int customerId) 
            this.customerId = customerId;
        

        public String getFeedbackText() 
            return feedbackText;
        
        public void setFeedbackText(String feedbackText) 
            this.feedbackText = feedbackText;
        
        public Date getApplicationDate() 
            return applicationDate;
        
        public void setApplicationDate(Date applicationDate) 
            this.applicationDate = applicationDate;
        
        public String getFunnelPageName() 
            return funnelPageName;
        
        public void setFunnelPageName(String funnelPageName) 
            this.funnelPageName = funnelPageName;
        
        public String getApplicationReferenceSource() 
            return applicationReferenceSource;
        
        public void setApplicationReferenceSource(String applicationReferenceSource) 
            this.applicationReferenceSource = applicationReferenceSource;
        
        public int getLanguageId() 
            return languageId;
        
        public void setLanguageId(int languageId) 
            this.languageId = languageId;
        

        public String getReasonForCancelation() 
            return reasonForCancelation;
        
        public void setReasonForCancelation(String reasonForCancelation) 
            this.reasonForCancelation = reasonForCancelation;
        
        @Override
        public String toString() 
            return "ApplicationAnswerDTO [answerId=" + answerId + ", applicationQuestionId="
                    + applicationQuestionId + ", recommendId=" + recommendId
                    + ", bookingId=" + bookingId + ", customerId=" + customerId
                    + ", reasonForCancelation="
                    + reasonForCancelation + ", feedbackText="
                    + feedbackText + ", applicationDate=" + applicationDate
                    + ", funnelPageName=" + funnelPageName
                    + ", applicationReferenceSource=" + applicationReferenceSource
                    + ", languageId=" + languageId + "]";
        


    

提前感谢您提供任何类型的信息和建议。

【问题讨论】:

你怎么称呼这个?有其他客户端或代码吗? 由于我附上了休息客户端的图像,我正在使用 post 方法的 URL 进行调用。并将我的 pojo 类的 Json 输入放在原始有效负载部分中。 错误通常是当你在'GET'支持的方法上调用'POST'方法时,重新检查你是否调用了正确的方法 【参考方案1】:
 @RequestMapping(value = "/saveApplicationAnswer", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public int saveApplicationAnswer(@RequestBody(ServicesConstants.SURVERY_ANSWER_FL) ApplicationAnswerDTO applicationAnswer) 

            LOG.info("ApplicationDBController fn saveApplicationAnswer BookingId   ",applicationAnswer.getBookingId(),  ServicesConstants.CUSTOMER_ID_FL, applicationAnswer.getCustomerId());
            return applicationDBService.saveapplicationAnswer(applicationAnswer);
        

我已经更改了引用 this post 的参数注释。

来自

@ModelAttribute(ServicesConstants.SURVERY_ANSWER_FL) ApplicationAnswerDTO applicationAnswer

@RequestBody(ApplicationAnswerDTO applicationAnswer 

它对我有用。似乎@RequestBody 是正确的。但我不知道@RequestBody@ModelAttribute 之间的区别。如果有人知道不同,请在这里分享。这对某些人会有帮助。

当我进站时@RequestBody(ApplicationAnswerDTO applicationAnswer) 它对我有用。

任何方式感谢大家的帮助和建议。

【讨论】:

【参考方案2】:
Can you check the method type which your are requesting.
In the screen shot which your shared it is displaying only get and head method are allowed.
I have tried your code in my Soap ui. It is displaying the below response.

HTTP/1.1 200 
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 04 Aug 2016 13:03:11 GMT

1999999999



It is displaying the response which you shared when i try to call the same service using Get method.Below is the response.

    
       "timestamp": 1470315684018,
       "status": 405,
       "error": "Method Not Allowed",
       "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
       "message": "Request method 'GET' not supported",
       "path": "/saveApplicationAnswer"
    

我使用的代码是

  
@RequestMapping(value = "/saveApplicationAnswer", method =        RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces =  MediaType.APPLICATION_JSON_VALUE)
public int saveApplicationAnswer(@ModelAttribute("hello")   ApplicationAnswerDTO applicationAnswer) 
    System.out.println(applicationAnswer);
    return 1999999999;
    

请尝试使用不同的工具,最好是soap ui。

【讨论】:

以上是关于Spring Boot 应用 Post 方法调用,405: Method Not Allowed的主要内容,如果未能解决你的问题,请参考以下文章

spring boot angular 2 post方法403错误

Spring Boot - 不允许使用 POST 方法

使用 AngularJS $http.post 登录 Spring Boot

使用 Spring Boot 的多个 REST 调用

Spring Boot - 模拟对外部 API 的 POST REST 请求

重启 Spring Boot 应用程序后第一次调用缓慢