Java Spring引导应用程序方法调用后,405:不允许方法
我正在用Spring boot编写Rest应用程序。将我的服务代码公开为REST服务 当我用下面的控制器代码和Pojo类代码编写Post方法时,我能够在GET方法中公开我的服务。我得到405:methodnotallowed错误。 不明白为什么 我也提到了这一点和其他相关的问题,但我不知道是什么问题 下面是我的控制器和带有jackson Json注释代码的Pojo 当我调用using和using as时,我得到以下错误 在同一个班级里,我有一些GET方法工作得很好。 错误: URL:Java Spring引导应用程序方法调用后,405:不允许方法,java,spring,rest,spring-boot,spring-restcontroller,Java,Spring,Rest,Spring Boot,Spring Restcontroller,我正在用Spring boot编写Rest应用程序。将我的服务代码公开为REST服务 当我用下面的控制器代码和Pojo类代码编写Post方法时,我能够在GET方法中公开我的服务。我得到405:methodnotallowed错误。 不明白为什么 我也提到了这一点和其他相关的问题,但我不知道是什么问题 下面是我的控制器和带有jackson Json注释代码的Pojo 当我调用using和using as时,我得到以下错误 在同一个班级里,我有一些GET方法工作得很好。 错误: URL:http:/
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
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注释的我的Pojo:
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 + "]";
}
}
提前感谢您提供的任何信息和建议
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
@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);
}
我已更改参数注释引用
从
到
这对我有用。似乎@RequestBody
是正确的。但是我不知道@RequestBody
和@modeldattribute
之间的区别。如果有人知道不同,请在这里分享。这对某些人来说是有帮助的
当我将@RequestBody(ApplicationAnswerDTO applicationAnswer)
放入时,它对我起了作用
无论如何,谢谢大家的帮助和建议。你怎么称呼这个?有rest客户端或代码吗?因为我附上了rest客户端的图片,所以我正在使用post方法的URL进行调用。并将我的pojo类的Json输入放在原始有效负载部分。通常会出现这样的错误:当您在“GET”支持的方法上调用类似“POST”的方法时,请重新检查是否调用了正确的方法
@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);
}
@ModelAttribute(ServicesConstants.SURVERY_ANSWER_FL) ApplicationAnswerDTO applicationAnswer
@RequestBody(ApplicationAnswerDTO applicationAnswer