Spring RestTemplate Post对象不发送请求吗? [英] Spring RestTemplate post object not not sending in request?

查看:322
本文介绍了Spring RestTemplate Post对象不发送请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestTemplate使用Web服务,我的POST请求如下.

I am consuming a web service using RestTemplate and my POST request is as follows.

   String url = "http://192.168.1.3:9200/_search";

RestTemplate requestTemplate = new RestTemplate();

    MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
            jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

            requestTemplate.getMessageConverters().add(jsonHttpMessageConverter);
    String uuidResult = requestTemplate.postForObject(url, requestBodyJsonObject ,
                    String.class);

我尝试打印我的 requestBodyJsonObject 并将其放在其他客户端中,效果很好,因此请求正文没有问题.但是在这里,我没有得到预期的结果,而只是在发送发布请求时得到了结果,而没有来自其余客户端的请求正文.

I tried printing my requestBodyJsonObject and put it in a rest client, it works fine so no issue with request body. But here I am not getting the expected result and just getting the result when I send a post request without a request body from rest client.

所以我在这里一无所知.非常感谢您的帮助.

So I am clueless here. Your help would be highly appreciated.

推荐答案

像这样简单调用,不要添加

call simple like this, dont add

MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();

带有休息模板 jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);

with rest template jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

样品:

RestTemplate requestTemplate = new RestTemplate();
    Login responseEntity=null;
            try {
                 responseEntity = restTemplate.postForObject("/http:localhost:8080/login", login,
                        Login.class);
            } catch (Exception e) {
            }
            return responseEntity;

这篇关于Spring RestTemplate Post对象不发送请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆