当我运行下面的curl命令(在Windows 7上)时,我收到一个错误(HTTP 415:不支持的媒体类型异常)。据我所知,泽西岛应该能够检测资源,因为我已经将内容类型指示为" application / xml"在卷曲请求中。有人可以帮我解决这个例外吗?
我使用以下curl命令:
curl -i -X POST -H "Content-type: application/xml" -d @sample-input.xml
http://localhost:8080/JAXRS/rest/Customer
我的JAXB注释模型类:
public class Customer {
@XmlAttribute(required=true)
protected int id;
@XmlElement(required=true)
protected String firstname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@XmlElement(required=true)
protected String lastname;
@XmlElement(required=true)
protected Address address;
@XmlElement(required=true)
protected String email;
@XmlElement (required=true)
protected String phone;
public Customer() { }
}
@XmlRootElement(name="address")
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
@XmlElement(required=true)
protected int number;
@XmlElement(required=true)
protected String street;
@XmlElement(required=true)
protected String city;
@XmlElement(required=true)
protected String state;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@XmlElement(required=true)
protected String zip;
@XmlElement(required=true)
protected String country;
public Address() { }
}
我使用JAX RS编写了以下资源
@POST
@Consumes({"application/xml","application/json"})
public Response createCustomerData(Customer customer){
try {
long customerId = 1;
return Response.created(URI.create("/" + customerId)).build();
} catch (Exception e) {
throw new WebApplicationException(e,
Response.Status.INTERNAL_SERVER_ERROR);
}
}
sample-input.xml的内容是:
<?xml version="1.0" encoding="UTF-8"?>
<customer id="1">
<firstname>Duke</firstname>
<lastname>OfJava</lastname>
<address>
<number>1</number>
<street>Duke's Way</street>
<city>JavaTown</city>
<state>JA</state>
<zip>12345</zip>
<country>USA</country>
</address>
<email>duke@example.com</email>
<phone>123-456-7890</phone>
</customer>
答案 0 :(得分:0)
问题解决了。我的客户模型类中缺少JAXB注释:
@XmlRootElement(名称= “客户”) @XmlAccessorType(XmlAccessType.FIELD)