English 中文(简体)
REST web服务中的Http 204错误(Jersey)
原标题:Http 204 error in REST web service (Jersey)

我正在使用Jersey/Java来开发我的REST服务。我需要为我的CarStore返回一个XML表示:

 @XmlRootElement
public class CarStore {
 private List<Car> cars;

 public List<Car> getCars() {
  return cars;
 }
 public void setCars(List<Car> cars) {
  this.cars = cars;
 }

这是我的Car对象:

@XmlRootElement
> public class Car {
 private String carName;
 private Specs carSpecs;
 private Category carCategory;
 public String getCarName() {
  return carName;
 }
 public void setCarName(String carName) {
  this.carName = carName;
 }
 public Specs getCarSpecs() {
  return carSpecs;
 }
 public void setCarSpecs(Specs carSpecs) {
  this.carSpecs = carSpecs;
 }
 public Category getCarCategory() {
  return carCategory;
 }
 public void setCarCategory(Category carCategory) {
  this.carCategory = carCategory;
 }

}

Specs和Category是如下枚举:

    @XmlRootElement
> public enum Category {

 SEDANS, COMPACTS, WAGONS, HATCH_HYBRIDS, SUVS, CONVERTIBLES, COMPARABLE;
}

我的资源类是:

    @GET
 @Produces({MediaType.APPLICATION_XML})
 public CarStore getCars()
 {
    return CarStoreModel.instance.getAllCars();
 }

我的球衣客户是:

WebResource service = client.resource(getBaseURI());
System.out.println(service.path("rest").path("cars").accept(
MediaType.APPLICATION_XML).get(String.class));

我在访问时遇到Http 204错误,同时出现客户端异常:

com.sun.jsersey.api.client.UniformInterface异常

有什么想法吗?谢谢

EDIT : I have yet not developed the model class...I just initialized some car objects as dummy data and put them in carstore. Showing all the classes here would be very clumsy. BTW, sorry for writing 204 Error..it is just that I am getting an Exception that led me think so.

最佳回答

我相信您得到了一个UniformInterfaceException,因为您的getCars()函数没有返回HTTP响应体。根本问题是JAXB没有将您的Car List转换为XML,因为它缺少@XmlElement注释。

您的getCars()函数应该是:

@GET
@Produces(MediaType.APPLICATION_XML)
public CarStore getCars() {
    // myCarStore is an instance of CarStore        
    return myCarStore.getCars();
}

并且您在CarStore中的汽车列表应定义为:

@XmlElement(name="car")
private List<Car> cars;
问题回答

我猜异常与响应代码(204)无关,因为204是指示“无内容”的成功条件

您返回的是xml格式的吗?我不确定getAllCars能做什么,但你可以使用Fiddler之类的工具来帮助你查看流量,查看返回给客户端的内容以及格式是否正确等

在您的客户端代码中,资源路径是否正确?请确保getBaseURI正在返回一个值。

也许可以尝试:

Client client = new Client();
WebResource resource = client.resource(getBaseURI());
CarStore carStore = resource.path("/rest/cars").accept(MediaType.APPLICATION_XML).get(CarStore.class);

你的资源类上没有缺少@Path注释吗?

@GET
@Path("cars")
@Produces({ MediaType.APPLICATION_XML })
public CarStore getCars() {
   return CarStoreModel.instance.getAllCars();
}

通过在getCars()方法中放置断点(或放置System.out.println)来检查REST WS安装的URL是否是您期望的URL,以确保它确实被调用。

在返回HTTP204时,Jersey中似乎有一个硬编码的检查来抛出UniformInterfaceException。

最好的解决方案是修复rest服务器,使其永远不会返回null。例如,返回一个空列表或一个未设置值的类。

否则,您将需要捕获UniformInterfaceException,这真的很难看

    if (getStatus() == 204) {
        throw new UniformInterfaceException(this);
    }

More info here : http://grepcode.com/file/repo1.maven.org/maven2/com.sun.jersey/jersey-client/1.17.1/com/sun/jersey/api/client/ClientResponse.java#ClientResponse.getEntity%28java.lang.Class%2Cjava.lang.reflect.Type%29





相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...

热门标签