ajax 传json数据 400 405 500。

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

image.png

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

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.3k
1 个回答

@RequestBody是把整个body传给方法参数,不是拆碎了传给方法的各个参数
你需要用一个类来接这个参数,可能长这样:

public class Request{
    int userUid;
    Date appointmentDate;
    Time startTime;
    Time endTime;
    int roomUid;
    int state;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题