400 错误的休息请求 application/xml
400 bad request for rest application/xml
正在使用 jersey 框架开发简单的 rest web 服务。编写的 @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 client
中出现的错误
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 文档。有效 XML 文档必须仅包含 1 个根节点。
这个
<?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 你会得到一个 return
<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>
我已经测试过了,它工作正常
正在使用 jersey 框架开发简单的 rest web 服务。编写的 @POST 方法,它消耗和生成 { MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML
}。使用 chrome Advance rest 客户端调用 Web 服务。对于以下 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 client
中出现的错误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 文档。有效 XML 文档必须仅包含 1 个根节点。
这个
<?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 你会得到一个 return
<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>
我已经测试过了,它工作正常