渺沧海一粟667
2021-08-30 15:00
采纳率: 100%
浏览 74
已结题

ajax 传json数据 400 405 500

json前端显示是传递过去的。

img

直接上报错。百度了好多都解决不了

30-Aug-2021 14:48:51.906 警告 [http-nio-8080-exec-9] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `int` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `int` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (PushbackInputStream); line: 1, column: 1]]

感觉是我代码错了。附上代码大伙帮忙看看。
可能错误 个人感觉
1.ajax传time类型值。冒号被解析成%3A
2.json后端的读取

//实体类属性有添加注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date appointmentDate;
@DateTimeFormat(pattern = "HH:mm:ss")
private Time startTime;
@DateTimeFormat(pattern = "HH:mm:ss")
private Time endTime;
//controller层用@RequestBody接收不知道写的对不对
    @RequestMapping("/orderAppointment")
    @ResponseBody
    public String orderAppointment(@RequestBody int userUid, @RequestBody Date appointmentDate,@RequestBody Time startTime, @RequestBody Time endTime, @RequestBody int roomUid, @RequestBody int state){
        int sum = userService.countAllAppointment();
        System.out.println("userUid:"+userUid+"appointmentDate:"+appointmentDate+"startTime:"+startTime+"endTime:"+endTime+"roomUid"+roomUid+"state:"+state);
        Appointment appointment = new Appointment(userUid, 300+sum, appointmentDate, startTime, endTime, roomUid, state);
        int res = userService.checkAppointment(appointment);
        if (res>0){
            return "fail";
        }else {
            int res1 = userService.addAppointmentInfo(appointment);
            int res2 = userService.addAppointment(appointment);
            if (res1>0&&res2>0){
                return "success";
            }else {
                return "fail";
            }
        }
    }

//ajax  发送ajax请求的时候前端能把json打印出来

                        $.ajax({
                            url: '/orderAppointment',
                            type: 'post',
                            contentType:'application/json;charset=utf-8',
                            dataType: 'text',
                            data: JSON.stringify({
                                'userUid': userUid,
                                'roomUid': roomUid,
                                'appointmentDate': appointmentDate,
                                'startTime': startTime,
                                'endTime': endTime,
                                'state': state
                            }),
                            success: function (res) {
                                if (res == "success") {
                                    layer.msg('预约成功!');
                                } else {
                                    layer.msg("对不起,预约失败.");
                                }
                            }
                        });
  • 收藏

2条回答 默认 最新

相关推荐 更多相似问题