简体   繁体   English

JAX-RS(REST轻松)InMemoryClientExecutor / ClientRequest异常

[英]JAX-RS (REST Easy) InMemoryClientExecutor/ClientRequest Exception

I'm trying to use the InMemoryClientExecutor to call services on my local JVM which return javax.ws.rs.core.Response objects. 我正在尝试使用InMemoryClientExecutor在我的本地JVM上调用服务,该服务返回javax.ws.rs.core.Response对象。 I can get this to work but only by specifying the String type like so: 我可以使它工作,但只能通过指定String类型,如下所示:

String response = new ClientRequest("/myService", executor)
    .get(String.class)
    .getEntity();

Since the signature of the service is like so: 由于服务的签名是这样的:

@GET
@Path("/myService")
public Response getSomeData(@Form MyFormBean bean) {
    //...blah...
}

However I would like to get the actual Response objects back so that I can get the entities out and return those objects in my new "aggregate" response. 但是,我想找回实际的Response对象,以便可以取出实体并在新的“聚合”响应中返回这些对象。 When I just return the string the Jackson Parser doesn't see this as JSON, just a regular string so it does things like encoding line breaks into \\n, etc. 当我只返回字符串时,Jackson Parser不会将其视为JSON,而只是常规字符串,因此它会执行诸如将换行符编码为\\ n等操作。

Whenever I do something like: 每当我做类似的事情:

String response = new ClientRequest("/myService",executor)
    .get(Response.class)
    .getEntity();

or even this to just get the plain request object: 或者甚至只是获取普通请求对象:

String response = new ClientRequest("/myService", executor).get().getEntity();

I get the following Exception: 我收到以下异常:

Exception Occured: org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type application/json and type null
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:523)
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:514)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:415)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:377)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:338)

Also present further down is: 同样出现在下方的是:

Servlet.service() for servlet Resteasy threw exception: org.jboss.resteasy.spi.LoggableFailure: Unable to find contextual data of type: javax.servlet.http.HttpServletRequest
    at org.jboss.resteasy.core.ContextParameterInjector$GenericDelegatingProxy.invoke(ContextParameterInjector.java:56)
    at $Proxy210.getScheme(Unknown Source)

Where am I going wrong? 我要去哪里错了?

There is no Response object being returned from the server, just a data stream that, in your case, is the representation of some entity, in JSON format. 没有从服务器返回的Response对象,只是一个以JSON格式表示的实体的数据流(在您的情况下)。 Trying to deserialize this data back into a Response object on the client side will not work, because effectively you are saying the JSON represents a Response object, which it doesn't (it represents the entity that *used to be contained in the Response). 尝试将该数据反序列化回客户端上的Response对象将不起作用,因为实际上您是在说JSON表示一个Response对象,而JSON不是(它表示*过去包含在Response中的实体) 。

Good news is, there are other ways to obtain the entity, which do not require trying to back-spin it into a Response . 好消息是,还有其他获取实体的方法,不需要尝试将其反向旋转为Response You will need to have the entities on the classpath of your client: 您将需要在客户端的类路径上包含实体:

MyEntity response = new ClientRequest("/myService", executor)
    .get(MyEntity.class)
    .getEntity();

Replace MyEntity with the type of the actual entity you are expecting to receive. MyEntity替换为您希望接收的实际实体的类型。

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

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