在Java Client应用程序中实现PUT

时间:2019-03-13 21:12:47

标签: java

我正在尝试使用Java实现宁静的客户端 但是当我使用PUT时,我会不断收到错误消息

Response response = invocationBuilder.put(Entity.entity(b,MediaType.APPLICATION_XML ));

我已经在Postman中检查了端点,并且工作正常。

代码:

public class Book {

    public static void main(String[] args)
    {

    Client client = ClientBuilder.newClient();

            WebTarget webTarget =client.target("http://localhost:8080/ServerSide/..... ");



            String[] n= new String[1];
            n[0]=MediaType.APPLICATION_XML;




            BookInfo b = new BookInfo();
            b.setId(1);
            b.setName("new book");

Invocation.Builder invocationBuilder =  webTarget.request(n);


Response response = invocationBuilder.put(Entity.entity(b,MediaType.APPLICATION_XML ));
  

错误:

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/xml, type=class BookInfo, genericType=class BookInfo.
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/xml, type=class BookInfo, genericType=class BookInfo.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
    at org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:517)
    at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:499)
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:393)
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
    at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
    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:444)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:326)
    at Book.main(Book.java:53)

2 个答案:

答案 0 :(得分:0)

您的项目中是否有此依赖项? Jersey告诉您,它不知道如何将实体'b'转换为在线XML。

<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-jaxb</artifactId>
  <version><!-- version here --></version>
</dependency>

这提供了必要的绑定。

答案 1 :(得分:0)

您无需使用自己的代码,而只能使用现有的3d客户端。这里是一些受欢迎的:

  1. Apache Http客户端:请参见此link
  2. 确定Http客户端:请参见此客户端:请参见link
  3. 最后是我最喜欢的一个(来自我编写的Open Source MgntUtils库):有关HttpClient的信息,请参见此javadoc。该库本身可作为Maven Artifact或从Github获得。它易于使用且经过良好测试。您需要做的就是根据需要使用方法setContentType(String)设置内容类型的值,然后仅调用方法sendHttpRequest(String, HttpMethod)sendHttpRequest(String, HttpMethod, String)(或者如果答复是二进制的,例如作为图像,然后调用方法sendHttpRequestForBinaryResponse(String, HttpMethod)sendHttpRequestForBinaryResponse(String, HttpMethod, String)