Spring不接受嵌入式json

时间:2014-04-24 19:20:19

标签: json spring spring-mvc jackson abstraction

我坚持使用JSON并需要相同的帮助。

我有一个类似下面的JSON,我从客户端获取。我正在使用Chrome rest客户端传递参数。内容类型是“application / json”

我的控制器和bean类如下。当我打电话时,我收到400错误的请求错误。请让我出错?除了使用这么多bean类之外,还有其他方法可以实现吗?

{
    "addSurveyRequest": {
        "data": {
            "suveyName": "abcde",
            "surveyDesc": "pqrst",
            "surveyFromDate": "MM-DD-YYYY",
            "surveyToDate": "MM-DD-YYYY",
            "surveyOptions": [
                {
                    "surveyOptionName": "abc",
                    "surveyOptionDesc": "qwerty"
                },
                {
                    "surveyOptionName": "pqr",
                    "surveyOptionDesc": "asdfg"
                }
            ]
        }
    }
}

我的控制器就像

@RequestMapping(value = "/add", method = RequestMethod.POST, consumes="application/json", produces="application/json")
public @ResponseBody String addSurvey(@RequestBody AddSurveyRequest addSurveyRequest) throws Exception
{
    String surveyId=null;
    logger.debug("surveyName"+addSurveyRequest.getDataDTO().getSurveyDTO().getSurveyName()+"surveyDesc "+addSurveyRequest.getDataDTO().getSurveyDTO().getSurveyDesc()+"fromdate"+addSurveyRequest.getDataDTO().getSurveyDTO().getSurveyFromDate());
}

我的bean类如下所示。

public class AddSurveyRequest{
    private DataDTO data;
    //setteres and getters
}

public class DataDTO{
    private SurveyDTO surveyDTO;
    //setteres and getters
}

public class SurveyDTO {
    private int surveyId;
    private String surveyName;
    private String surveyDesc;  
    private Date surveyFromDate;
    private Date surveyToDate;
    private List<SurveyOptionDTO> surveyOptions;
    //setteres and getters
}

public class SurveyOptionDTO {
    private String surveyOptionName;
    private String surveyOptionDesc;
    //setteres and getters
}

任何帮助非常感谢。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我在我的Java类中使用与JSON参数名称相同的变量名称,这对我来说很有用。我从另一个链接得到了这个答案,错过了链接。

以下是我所做的更改并且有效!

public class AddSurveyRequest{ private DataDTO addSurveyRequest; //setteres and getters }

public class DataDTO{ private SurveyDTO data; //setteres and getters }