我正在创建一个Web服务,它接受酒店管理系统的JSON
对象,如姓名,电子邮件,电话等,并使用Java Object提取它。
@Path("/get")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class AppResource {
@POST
public Response setHotel(Hotel hotel) {
String output = hotel.toString();
System.out.println(output);
return Response
.created(null)
.build();
}
}
我的Java对象类是
public class Hotel {
private String hotelName;
private String email;
private String phone;
private String address;
private String geoLocation;
// Must have no-argument constructor
public Hotel()
{
}
public Hotel(String hotelName,String email,String phone,String address,String geoLocation) {
this.hotelName = hotelName;
this.email = email;
this.phone = phone;
this.address = address;
this.geoLocation = geoLocation;
}
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGeoLocation() {
return geoLocation;
}
public void setGeoLocation(String geoLocation) {
this.geoLocation = geoLocation;
}
@Override
public String toString() {
return new StringBuffer(" Hotel Name : ").append(this.hotelName)
.append(" Email : ").append(this.email)
.append(" Phone : ").append(this.phone).append(" GeoLocation : ")
.append(this.geoLocation).toString();
}
但是当我调用此Web服务时,我收到此错误
ERROR [2015-08-13 18:17:47,143] com.sun.jersey.spi.container.ContainerRequest: A message body reader for Java class com.quinchy.org.Hotel, and Java type class com.quinchy.org.Hotel, and MIME media type application/octet-stream was not found.
The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
application/octet-stream ->
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
我正在使用此
创建请求Hotel st = new Hotel("Ashish","ashish","9910687844","Delhi","23.32,356");
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(
JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(clientConfig);
WebResource webResource = client
.resource("http://localhost:8080/get");
ClientResponse response = webResource.accept("application/json")
.type("application/json").post(ClientResponse.class, st);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Server response .... \n");
System.out.println(output);