jsongroovyuniresthipchat

Hipchat Message with Groovy - Can't find an ObjectMapper implementation


I'm having trouble with a Groovy script, attempting to post a message in Hipchat with Unirest.

Caught: java.lang.RuntimeException: Serialization Impossible. Can't find an ObjectMapper implementation.
java.lang.RuntimeException: Serialization Impossible. Can't find an ObjectMapper implementation.
at com.mashape.unirest.request.HttpRequestWithBody.body(HttpRequestWithBody.java:155)
at com.mashape.unirest.request.HttpRequestWithBody$body.call(Unknown Source)
at 011.run(011.groovy:15)

Thats the Script:

@Grab(group='com.mashape.unirest', module='unirest-java', version='1.4.9')
import com.mashape.unirest.http.JsonNode
import com.mashape.unirest.http.HttpResponse
import com.mashape.unirest.http.Unirest

def apiToken = " [Token] "

Unirest.clearDefaultHeaders()

Unirest.post("https://api.hipchat.com/v2/room/ [Number] /message" )
.header("Content-Type", "application/json" )
.queryString('auth_token', apiToken)
.body(["message": "Test", "notify": True])
.asString()

Thank you in advance for your help.


Solution

  • You are passing a Map to .body(...), but the doc says it expects either a String, or a JsonNode, or an Object, and for Objects you will need more configuration to specify how they are serialized (and a Map falls in that category).

    Maybe you can tell Groovy to generate a JSON String value for you, from your Map object:

    .body(JsonOutput.toJson(["message": "Test", "notify": true]))
    

    (JsonOutput is in package groovy.json)