【发布时间】:2015-01-13 13:17:20
【问题描述】:
我正在使用 jersey 框架开发简单的 rest web 服务。书面的@POST 方法,它消耗和产生{MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML}。使用 chrome Advance rest 客户端调用 Web 服务。对于以下应用程序/json 请求,Webservices 正在按预期工作
{"noSave": "Save", "dateCre":"14/12/2014"}
但收到以下应用程序/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>
【问题讨论】:
-
向我们展示错误详情。
-
状态 400 错误请求显示说明 加载时间:1013 请求标头用户代理:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/ 537.36 来源:chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo 内容类型:application/xml 接受:/ 接受编码:gzip,放气接受语言:en-GB,en-US;q=0.8, en;q=0.6 响应标头 服务器:Apache-Coyote/1.1 内容类型:text/html;charset=utf-8 内容语言:en 内容长度:990 日期:2015 年 1 月 13 日星期二 13:29:46 GMT连接:关闭
-
上面是显示的错误信息。
-
请edit your question 并在此处包含错误消息。还请包括相关的日志输出。
标签: xml rest jersey jax-rs jersey-2.0