0

I'm trying to create a restful service and I'm trying to consume my JSON response and convert it back to an Object.

Controller method

def mergeVendors(String region) {

    def report = new VendorReport();
    //do something with  report

    response.status = 201
    response ([vendorReport: report]) as JSON
}

Test Method

void "Test Merge Vendors"() {
    when:       
    controller.request.method = 'POST'
    controller.request.json = '[{id:1, zip:"14224"}]'

    controller.mergeVendors("Florida")

    def response =  controller.response


    then:
    response.zip == "14224"
}

The code above is what I'm trying to use and I get the following exception. How do I cast the response back to a VendorReport obj?

groovy.lang.MissingMethodException: No signature of method: org.healthresearch.VendorController.response() is applicable for argument types: (java.util.LinkedHashMap) values: [[vendorReport:org.vendor.VendorReport@1b0dbdf1]] Possible solutions: respond(java.lang.Object), getResponse(), respond(java.lang.Object, java.util.Map), respond(java.util.Map, java.lang.Object), respondsTo(java.lang.String), respondsTo(java.lang.String, [Ljava.lang.Object;)

1 Answer 1

3
response ([vendorReport: report]) as JSON

change to

render ([vendorReport: report]) as JSON
Sign up to request clarification or add additional context in comments.

4 Comments

What do I need to do to cast the json back to a VendorReport object in my test?
then: response.json == report
then: def response = controller.response.json; response == report Test Merge Vendors(org.VendorControllerIntegrationSpec) | org.codehaus.groovy.grails.web.converters.exceptions.ConverterException: Error parsing JSON at grails.converters.JSON.parse(JSON.java:278) at org.VendorControllerIntegrationSpec.Test Merge Vendors(VendorControllerIntegrationSpec.groovy:32) Caused by: org.codehaus.groovy.grails.web.json.JSONException: Expected a ',' or ']' at character 16 of ['vendorReport':org.vendor.VendorReport@1e5c61e6] at grails.converters.JSON.parse(JSON.java:269) ... 1 more
I don't know the structure of your VendorReport class. You might need a GSP template to generate proper JSON. Debug your app, check if your JSON is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.