messagebodyreader jersey

时间:2013-03-29 09:39:22

标签: java web-services jersey jax-rs

我正在创建一个用于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;
    }
} 

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

1 个答案:

答案 0 :(得分:3)

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

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

如果您只想发送测试字符串,则无需将其换行。 String是 Jersey 中的默认实体类型。

相关问题