正在使用 Jersey 框架开发简单的休息网络服务。编写的 @POST 方法使用并生成 { MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML
}。使用 chrome Advance Rest 客户端调用 Web 服务。对于以下 application/json 请求,Web 服务按预期工作
{"noSave": "Save", "dateCre":"14/12/2014"}
但是以下 appliation/xml 请求收到 400 错误请求
<?xml version="1.0" encoding="UTF-8">
<noSave>Save</noSave>
<dateCre>14/12/2014</dateCre>
代码中没有编译错误。感谢您对解决以下问题的任何帮助。下面是我写的代码
请求对象:
import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Creation", propOrder = {
})
public class Creation {
@XmlElement(required = true)
protected String noSave;
@XmlElement(required = true)
protected String dateCre;
public String getNoSave() {
return noSave;
}
public void setNoSave(String value) {
this.noSave = value;
}
public String getDateCre() {
return dateCre;
}
public void setDateCre(String value) {
this.dateCre = value;
}
}
响应对象:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MyResponse", propOrder = {
})
public class MyResponse {
@XmlElement(required = true)
protected String resString;
public String getResString() {
return resString;
}
public void setResString(String value) {
this.resString = value;
}
}
休息网络服务:
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/create")
public class CreateRequest {
@POST
@Produces({ MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createReq( @Valid Creation request)
{
System.out.println(" Request "+request.getNoSave());
MyResponse result = new MyResponse();
result.setResString(" Created with Number 123");
return Response.ok(result).build();
}
}
下面是 chrome Rest 客户端出现的错误
Status 400 Bad Request. Loading time: 4250
Request headers User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/xml
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Response headers Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 990
Date: Tue, 13 Jan 2015 13:53:11 GMT
Connection: close
我也尝试了下面的代码
@XmlRootElement(name="Creation")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Creation", propOrder = {
})
public class Creation {
以及下面的 xml 请求。但遇到同样的错误
<?xml version="1.0" encoding="UTF-8">
<Creation>
<noSave>NoSave</noSave>
<dateCre>14/12/2014</dateCre>
</Creation>
日志中显示以下错误
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Creation"). Expected elements are <{http://check.com/rrs}Creation>
最佳答案
这个
<?xml version="1.0" encoding="UTF-8">
<noSave>Save</noSave>
<dateCre>14/12/2014</dateCre>
不是有效的 XML。 XML 文档应该只有一个 根元素。对于 POJO 映射,根元素应该是 <Creation>
。所以尝试一下
<?xml version="1.0" encoding="UTF-8"?>
<Creation>
<noSave>sdfshd</noSave>
<dateCre>3840389</dateCre>
</Creation>
您还应该添加@XmlRootElement
到你的类(class)
@XmlRootElement(name = "Creation")
public class Creation {
同样适用于MyResponse
@XmlRootElement(name = "MyResponse")
public class MyResponse {
使用 XML,您将获得返回
<MyResponse>
...
</MyResponse>
没有办法解决这个问题。这就是 XML 的工作原理。
更新
您还缺少 ?
在 xml header 的末尾。
<?xml version="1.0" encoding="UTF-8"?> // See the `?`. You are missing that
尽管 header 甚至不是必需的。只需发送
<Creation>
<noSave>sdfshd</noSave>
<dateCre>3840389</dateCre>
</Creation>
我已经测试过了,效果很好
关于xml - 400 对剩余应用程序/xml 的错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923103/