lient请求时找不到合适的HttpMessageConvert

lient请求时找不到合适的HttpMessageConvert

本文介绍了尝试执行restclient请求时找不到合适的HttpMessageConverter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Spring for Android rest client 来发送带有 http post 的数据,以避免创建和解析json数据。

I'm trying to use Spring for Android rest client to send data with an http post , to avoid creating and parsing the json data.

从他们的他们有以下方法:

From their manual they have the following method:

restTemplate.postForObject(url, m, String.class)

调用该方法后,我得到以下异常:

After the method is called I get the following exception:

No suitable HttpMessageConverter found when trying to execute restclient request

我的活动代码段是:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
Message m = new Message();
m.setLibrary("1");
m.setPassword("1395");
m.setUserName("1395");
String result = restTemplate.postForObject(url, m, String.class);

Message对象是:

And the Message object is :

public class Message {
    private String UserName, Password, Library;

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public String getLibrary() {
        return Library;
    }

    public void setLibrary(String library) {
        Library = library;
    }
}

为什么不能将Message对象转换为JSON

推荐答案

可能有几个不同的原因导致这种情况发生。在我的情况下,我已经连接了RestTemplate,但仍然遇到此错误。事实证明,我必须添加对jackson-databind的依赖:

There could be a few different reasons why this can happen. In my case, i had the RestTemplate already wired in, but still got this error. Turns out, i had to add a dependency on "jackson-databind":

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

这篇关于尝试执行restclient请求时找不到合适的HttpMessageConverter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:37