0

So I have been harldy looking for an answer to this with no success.

I want to receive a "complex" param as JSON at my GET method.

How could I do it?

This is the method:

@RequestMapping(method = RequestMethod.GET, value = "/test2", consumes = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public ResponseEntity<?> test2(@RequestParam PojoParam pojoParam) {
    return new ResponseEntity<>(pojoParam, HttpStatus.OK);
}

This is the PojoParam:

public class PojoParam implements java.io.Serializable {
    private String stringVariable;
    private int intVariable;

    public PojoParam() {
    }

    public String getStringVariable() {
        return stringVariable;
    }

    public int getIntVariable() {
        return intVariable;
    }

    public void setStringVariable(String stringVariable) {
        this.stringVariable = stringVariable;
    }

    public void setIntVariable(int intVariable) {
        this.intVariable = intVariable;
    }
}

Invoke example:

... /test2?pojoParam={intVariable:2, stringVariable:"as"}

Response example:

{
    "timestamp": "2016-06-16T15:09:36Z",
    "status": 500,
    "error": "Internal Server Error",
    "exception": "org.springframework.beans.ConversionNotSupportedException",
    "message": "Failed to convert value of type 'java.lang.String' to required type 'com.htravel.tng.pe.resources.PojoParam'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.htravel.tng.pe.resources.PojoParam]: no matching editors or conversion strategy found",
    "path": "/rest/availability/test2"
}

I have tried with @RequestPart too without succes.

Any suggestions?

Thank you.

1 Answers1

0

What about getting it as a String and convert it to JSON Object after ?

Like suggest here for exemple.

Community
  • 1
  • 1
J.Mengelle
  • 341
  • 3
  • 11
  • Than you for your answer. The thing is, if I switch that method to a POST one and anotate the parameter as @RequestBody, It works perfectly fine. But I want it works as GET method and not setting it as String, nor using ObjectMapper.... I don't now if it is possible. – Daniel Martín Calvo Jun 16 '16 at 13:24
  • Oh, i was thinking you didn't have a specific Java class for this parameter. Just remove your annotation @RequestParam – J.Mengelle Jun 16 '16 at 13:44
  • Yep in this simple example that would work but the request would be: /test2?intVariable=2&stringVariable=as OK. But when this parameter is a more complex object I have the same problem. Imagine PojoParam has another variable which is an object with another int variable and another String variable. Same problem. – Daniel Martín Calvo Jun 16 '16 at 14:11
  • Well, if you want to stuck go GET request, you can't give json into a parameter without parsing it yourself. You need named parameter for each field of your bean. For nested bean, i don't know if the "a.b.c" notation could help, or you need to use a trick with setter that will do the job. – J.Mengelle Jun 16 '16 at 15:16