【问题标题】:messagebodyreader jerseymessagebodyreader 运动衫
【发布时间】:2013-03-20 00:08:00
【问题描述】:

我正在为restful webservice制作一个java客户端,我想在请求正文中发送一个字符串。

这是我的课。

 public class params {
    private String test;

  public String getTest() {
    return test;
  }

  public void setTest(String test) {
    this.test = test;
  }

这是我的主要功能类。

 public class testclient implements MessageBodyReader<params> {
    public static void main(String[] args) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());
        params pobj = new params();
        pobj.setTest("myname");
        System.out.println(service.path("interface").post(params.class);
     }

      private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/ivrservices").build();
      }

    public boolean isReadable(Class<?> params, Type genericType, Annotation[] arg2,
            MediaType arg3) {
        return false;
    }

    public params readFrom(Class<params> arg0, Type arg1,
            Annotation[] arg2, MediaType arg3,
            MultivaluedMap<String, String> arg4, InputStream arg5)
            throws IOException, WebApplicationException {
        // TODO Auto-generated method stub
        return null;
    }
} 

我在默认函数中传递什么参数?

【问题讨论】:

    标签: java web-services jersey jax-rs


    【解决方案1】:

    您似乎完全误读了 MessageBodyReader 的用法。它由 Provider 而非客户端来实现。对于您的情况,不需要自定义提供程序。例如,您可以使用带有 POJO 功能的 Jackson Json 提供程序来发送/接收 params。 因此,您的客户端配置将是:

    ClientConfig cc = new ClientConfig().register(JacksonFeature.class)
    这会将 params 序列化为 Json。 不要忘记在服务器上注册 JacksonFeature 以及反序列化请求。

    如果您只想发送 test 字符串,则不需要包装它。 String 是 Jersey 中的默认实体类型。

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多