正在开发简单的休息网络服务,并采用球衣框架。写入@POST方法,消耗并生成{MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML
}。使用chrome Advance rest客户端调用Web服务。 Webservices正如预期的那样运行以下application / json请求
{"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>
答案 0 :(得分:3)
此
<?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
@XmlRootElement(name = "Creation")
public class Creation {
MyResponse
@XmlRootElement(name = "MyResponse")
public class MyResponse {
使用XML,您将获得返回
<MyResponse>
...
</MyResponse>
没办法解决这个问题。这就是XML的工作原理。
您在xml标头的末尾也缺少?
。
<?xml version="1.0" encoding="UTF-8"?> // See the `?`. You are missing that
虽然甚至不需要标题。只需发送
<Creation>
<noSave>sdfshd</noSave>
<dateCre>3840389</dateCre>
</Creation>
我已经测试了这个并且工作正常
答案 1 :(得分:0)
可能会尝试将您的请求解析为xml文档。有效的XML文档必须只包含1个根节点。