Ajax 415不支持的媒体类型”;错误
我在使用SpringMVC将JSON从我的表单转换到我的工作中时遇到了问题。我的表单是动态的,JSON作为保存的对象返回。在处理案例时,不断出现415错误,即不支持媒体类型。请帮助我,下面是我的代码流程。我也尝试了不同的方法,但都不适用于我,我哪里做错了 HTML表单Ajax 415不支持的媒体类型”;错误,ajax,spring-mvc,Ajax,Spring Mvc,我在使用SpringMVC将JSON从我的表单转换到我的工作中时遇到了问题。我的表单是动态的,JSON作为保存的对象返回。在处理案例时,不断出现415错误,即不支持媒体类型。请帮助我,下面是我的代码流程。我也尝试了不同的方法,但都不适用于我,我哪里做错了 HTML表单 <form:form modelAttribute="bp" action="saveUser" method="post" name="bpForm" id="submitForm">
<form:form modelAttribute="bp" action="saveUser" method="post"
name="bpForm" id="submitForm">
<div class="form-group form-inline">
<label>Name</label>
<form:input type="text" path="bpName" name="bpName"
class="form-control" />
</div>
<button type="submit" class="btn btn-success"
>save</button>
</form:form>
####################################
模范班
@Entity
@Table(name="bp_master")
public class BusinessPartner {
@Id
//@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="BPId")
private int bpId;
@Column(name="BPName")
private String bpName;
public BusinessPartner(){
}
public BpType getBpType() {
return bpType;
}
public void setBpType(BpType bpType) {
this.bpType = bpType;
}
public int getBpId() {
return bpId;
}
public void setBpId(int bpId) {
this.bpId = bpId;
}
public String getBpName() {
return bpName;
}
public void setBpName(String bpName) {
this.bpName = bpName;
}
}
在控制器中,尝试修改方法,添加
consumes
属性,并删除HttpServletRequest
参数:
@RequestMapping(value="/saveUser", method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody BusinessPartner submittedFromData(@RequestBody BusinessPartner user) {
System.out.println("!!!!!!!!!!!! ==================="+user.getBpId());
return user;
}
以您的html格式:
<div class="form-group form-inline">
<label>Name</label>
<form:input type="text" path="bpName" name="bpName"
class="form-control" />
<form:input type="hidden" path="bpId" name="bpId"
class="form-control" />
</div>
及
@RequestMapping(value=“/saveUser”,method=RequestMethod.POST)
public@ResponseBody业务伙伴从数据(@RequestBody多值映射数据)提交{
Map params=data.toSingleValueMap();
System.out.println(“!!!!!!!!!!!!!!!!!================”+params.get(“bpName”);
返回用户;
}
我注意到您代码中的一些要点
我刚刚使用了这个maven依赖项和415响应代码。仅删除了重复的contentType:“application/json”,现在已对其进行编辑
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
com.fasterxml.jackson.core
杰克逊数据绑定
2.5.3
现在在我这边工作正常。hmm我认为问题在于Jquery调用您的bean包含两个字段,但在您的查询中,您只发送一个带有一个字段的对象。我对entity类的所有字段做了另一个示例。即使这样也显示出同样的错误。这是示例一==>数据:{“名称”:“Elecman私人有限公司ATF”,“dob”:“2018-02-14”,“电子邮件”:pavan@srini.com.au,“phone”:“3434353455”}更新您的建议后,再次出现415错误,您能告诉我出现此错误的可能性吗?您的请求中有一个错误的json格式:“bpType.bpTypeId”不支持是的,我已经添加了这一个,这在我的案例中起作用。非常感谢。
<div class="form-group form-inline">
<label>Name</label>
<form:input type="text" path="bpName" name="bpName"
class="form-control" />
<form:input type="hidden" path="bpId" name="bpId"
class="form-control" />
</div>
$.ajax({
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
type: frm.attr('method'),
url: frm.attr('action'),
dataType : 'application/x-www-form-urlencoded',
data : data ,
success : function(callback){
alert("Response: Name:");
// $(this).html("Success!");
},
error : function(){
// $(this).html("Error!");
}
@RequestMapping(value="/saveUser", method = RequestMethod.POST)
public @ResponseBody BusinessPartner submittedFromData(@RequestBody MultiValueMap<String, String > data) {
Map<String, String> params = data.toSingleValueMap();
System.out.println("!!!!!!!!!!!! ==================="+params.get("bpName"));
return user;
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>