java – Spring启动应用程序Post方法调用,405:方法不允许

我正在用Spring启动编写Rest Application.将我的服务代码公开为REST服务.

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

我已经提到这个link.和其他相关的但我无法弄清楚是什么问题.

下面是我的控制器和Pojo与杰克逊Json注释的代码.

当我使用Advanced REST client – Chrome Web Store – Google呼叫并使用attached image时,我收到以下错误.

在同一个类我有一些工作正常的GET方法.
错误:
 URL: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 + "]";
        }


    }

enter image description here

提前感谢您提供任何信息和建议.

最佳答案
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;
    }

请尝试使用不同的工具,最好是肥皂ui.

转载注明原文:java – Spring启动应用程序Post方法调用,405:方法不允许 - 代码日志