我正在使用泽西编写REST客户端。 我的REST api使用自定义邮件正文编写器。我使用POSTMAN获得正确的输出但是当我尝试编写客户端时,我得到了一个例外。
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
public class PetClient {
public static void main(String[] args) {
Client restClient = ClientBuilder.newClient();
Response response = restClient.target("http://localhost:8085/bootstrapexample/webapi/pet/dog/rani").request(MediaType.TEXT_PLAIN).get();
Pet pet = response.readEntity(Pet.class);
System.out.println(pet);
}
}
REST客户端代码:
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=class com.example.bootstrapexample.Pet, genericType=class com.example.bootstrapexample.Pet.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:231)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1075)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:111)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:108)
at com.example.bootstrapexample.PetClient.main(PetClient.java:12)
例外:
{{1}}
答案 0 :(得分:1)
您只有MessageBodyWriter
来处理服务器端的传出序列化,但传入反序列化需要MessageBodyReader
。
实施阅读器后,请使用Client
restClient.register(MyReader.class);
顺便说一下,它必须使提供程序类实现MessageBodyReader
和MessageBodyWriter
的通用模式。这样你只需要一个读写的类提供者。