如何在@RequestMapping上接收Json对象作为参数(method = RequestMethod.GET)

时间:2016-06-16 13:14:44

标签: java json rest

所以我很难找到答案但没有成功。

我想在我的GET方法中收到一个“复杂”的参数作为JSON。

我怎么能这样做?

这是方法:

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

这是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;
    }
}

调用示例:

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

回应示例:

{
    "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"
}

我也试过@RequestPart而没有成功。

有什么建议吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如何将它作为String并将其转换为JSON对象?

就像例如here一样。