question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cannot find symbol setObjectMapper in class Unirest

See original GitHub issue

while trying to set the object mapper for Unirest using ` Unirest.setObjectMapper(new ObjectMapper() { private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper();

        public <T> T readValue(String value, Class<T> valueType) {
            try {
                return jacksonObjectMapper.readValue(value, valueType);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        
        public String writeValue(Object value) {
            try {
                return jacksonObjectMapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    });

`

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
peter-snrcommented, Nov 20, 2019

@ryber thank you very much for your feedback, I finally have a solution, yes you were right the version I was using was well out of date.

My gradle dependencies are now: -

    // https://mvnrepository.com/artifact/com.konghq/unirest-java
compile 'com.konghq:unirest-java:3.1.04'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.4'

My configuration class does this…

@Configuration
public class UnirestConfig {
    @Autowired
    private com.fasterxml.jackson.databind.ObjectMapper mapper;

    @PostConstruct
public void postConstruct() {
    Unirest.config().setObjectMapper(new ObjectMapper() {

        public String writeValue(Object value) {
            try {
                    return mapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
    	        throw new RuntimeException(e);
            }
        }
            public <T> T readValue(String value, Class<T> valueType) {
            try {
                    return mapper.readValue(value, valueType);
                } catch (Exception e) {
                    throw new RuntimeException(e);
            }
        }
     });
       }
}

My controller class can now use Unirest with Java Classes

         HttpResponse<JsonNode> createUserResponse = Unirest.post(auth0UserCreateEndpoint)
                        .header("content-type", "application/json")
                        .header("authorization", "Bearer " + token)
                        .body(newUser)     // <== this is a User class instance
                        .asJson();

Works a treat, very happy with the results.

0reactions
rybercommented, Nov 19, 2019

try using a modern version. Note that we changed groupId and everything

<!-- Pull in as a traditional dependency -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.02</version>
</dependency>

<!-- OR as a snazzy new standalone jar with shaded dependencies -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.02</version>
    <classifier>standalone</classifier>
</dependency>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mashape Unirest Java : java.lang.NoClassDefFoundError
I did Maven Install by right clicking pom.xml and it generates a folder named target contains set of files with snapshot.jar file. You...
Read more >
A Guide to Unirest - Baeldung
Discover Unirest - a lightweight HTTP client library. ... Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:....
Read more >
Unirest in Java: Simplified, lightweight HTTP client library.
For this you need to provide the Unirest configuration with a implementation of ObjectMapper (see Object Mappers for details.). If the response is...
Read more >
Kong/unirest-java - Gitter
I can't get deserialization to work using the ObjectMapper example on the home ... ObjectMapper has signature readValue(String, Class<T>) while Unirest only ...
Read more >
com.mashape.unirest.request.HttpRequestWithBody.body ...
Can't find an ObjectMapper implementation."); } return body(objectMapper.writeValue(body)); }. origin: com.mashape.unirest/unirest-java ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found