我正在使用Spring启动编写Rest Application。将我的服务代码公开为REST服务。
当我使用下面的Controller和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
是我的上下文名称
我在server.context-path=/DBService
application.properties
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 + "]";
}
}
提前感谢您提供任何信息和建议。
答案 0 :(得分:1)
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。
答案 1 :(得分: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)
它对我有用。
任何方式感谢每一位人士的帮助和建议。