解析jersey client api调用接口出现的A message body reader/writer was not found异常

在项目中使用jesey client api访问接口的时候,出现了2种异常信息。分别如下:

  1. com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class com.xxx.ResultData, and Java type class com.xxx.ResultData, and MIME media type application/json; charset=UTF-8 was not found
  2. com.sun.jersey.api.client.ClientHandlerException:  A message body writer for Java type, class java.util.HashMap, and MIME media type, application/json, was not found
现在假如接口用restclient工具测试正常返回,然后那么用jesey client api来测试,测试代码如下:

public class JerseyClientTest {

    private static Client client;
    private static String targetURL = "http://192.168.0.175:8081/api/test";
    private static int status;    
    static ResultData<?> result;
    
    
    static{
        ClientConfig cc = new DefaultClientConfig();        
        client = Client.create(cc);
        client.setConnectTimeout(30000);
        client.setReadTimeout(30000);
    }
    
    
    public String doPost(Object input){
        try {
            WebResource webResource = client.resource(targetURL);
            ClientResponse response = webResource.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class,input);
            status = response.getStatus();
            System.out.println("status code: "+status);
            result = response.getEntity(ResultData.class);
            System.out.println("result data: "+result.toJsonString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "test";
    }

    
    @Test
    public void testPostWithJSON() {
        System.out.println("使用json对象提交请求:\n"+JSONHelper.map2json(_paramsMap()).toString());
        doPost(JSONHelper.map2json(_paramsMap()));
    }
    
    @Test
    public void testPostWithJSONString() {
        System.out.println("使用json字符串提交请求:\n"+JSONHelper.map2json(_paramsMap()).toString());
        doPost(JSONHelper.map2json(_paramsMap()).toString());
    }
    
    @Test
    public void testPostWithMap() {
        System.out.println("使用map对象提交请求:\n"+_paramsMap().toString());
        doPost(_paramsMap());
    }
    
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值