简体   繁体   English

带有GenericType的RESTful Web服务

[英]RESTful web service with GenericType

here is the first part of my code : 这是我的代码的第一部分:

@Path("compute/{x}")
@GET
@Produces(MediaType.TEXT_PLAIN) // "text/plain"
public List<Double> compute(@PathParam("x") Double x) {
    List<Double> list = new Vector<Double>();
    list.add(Math.log(x));
    list.add(Math.exp(x));
    return list;
}

I am learning/building a RESTful web service. 我正在学习/构建一个RESTful Web服务。 The client exploiting that RESTful web service is doing like this. 利用该RESTful Web服务的客户端就是这样做的。

javax.ws.rs.client.Client client = ClientBuilder.newClient();

WebTarget uri_basique = client
        .target("http://localhost:8080/Rest_ws/rest");

WebTarget targeted_resource = uri_basique.path("simple/compute/{x}")
        .resolveTemplate("x", 5);

Builder builder = targeted_resource.request(MediaType.TEXT_PLAIN);
List<Double> compute = builder.get(new GenericType<List<Double>>(){});

System.out.println("log (5) = " + compute.get(0));
System.out.println("exp (5) = " + compute.get(1));

It is supposed to work. 它应该工作。 But when I execute I get: 但是当我执行时,我得到:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    ...
    at ws_rs.Client.test3(Client.java:26)

The problematic line is the line : 有问题的行是:

List<Double> compute = builder.get(new GenericType<List<Double>>(){});

If someone can provide any explanation/solution to me. 如果有人可以向我提供任何解释/解决方案。

HTTP 500 Internal Server Error means that there is some error while calling the rest api. HTTP 500内部服务器错误表示调用其余api时出现一些错误。 It seems that the web service is not able to translate list as "text/plain" and hence it is throwing error. 似乎Web服务无法将列表转换为“文本/纯文本”,因此会引发错误。 Jersey does not provide MessageBodyReader/Writer to translate java.util.List into "text/plain". Jersey不提供MessageBodyReader/Writer来将java.util.List转换为“文本/纯文本”。 You have to provide a custom MessageBodyReader/Writer for this. 您必须为此提供一个自定义MessageBodyReader/Writer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM