杰克逊无法阅读文档:无法识别的标记'contactForm':期待('true','false'或'null')

问题描述 投票:3回答:2

我想将一个带有JQuery的POST请求发送到Spring Controller,但我一直从jquery中得到这个错误

Could not read document: Unrecognized token 'contactForm': was expecting ('true', 'false' or 'null')
 at [Source: java.io.PushbackInputStream@38220bcd; line: 1, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'contactForm': was expecting ('true', 'false' or 'null')
 at [Source: java.io.PushbackInputStream@38220bcd; line: 1, column: 13]

这是POST请求

$('#contactForm').on('submit', function(e){
        e.preventDefault();
        var contactForm = new Object;
        var firstName = $('#firstName').val();
        var lastName = $('#lastName').val();
        var email = $('#email').val();
        var message = $('#message').val();
        contactForm.firstName = firstName;
        contactForm.lastName = lastName;
        contactForm.email = email;
        contactForm.message = message;
        contactForm.accepted = true;
        console.log(JSON.stringify(contactForm));
        $.ajax({
            type: 'POST',
            url: '/checkContact.json',
            contentType : 'application/json; charset=utf-8',
            dataType: 'json',
            data: {
                contactForm: JSON.stringify(contactForm)

            },
            success: function(response){
                console.log(response)
                $('#success').text(response.message);
            },
            error: function(data){
                console.log(data.responseJSON.message);
            }
        })
    })

这是控制器

 @PostMapping("/checkContact.json")
       public @ResponseBody String sendContactForm(@Valid @RequestBody ContactForm contactForm, BindingResult result, HttpServletRequest request) throws MalformedURLException, JsonProcessingException{

//logic here

}

和ContactForm

public class ContactForm {

    @NotNull
    @NotEmpty
    @ValidEmail
    private String email;

    @NotNull
    @NotEmpty
    private String firstName;

    @NotNull
    @NotEmpty
    private String lastName;

    @NotNull
    @NotEmpty
    private String message;

//  @AssertTrue
    private boolean accepted;

//getters and setters
}

我不确切知道发生了什么,因为如果我,例如,尝试向控制器发送一个带有POSTMAN的JSON,这个与JSON.stringify(contactForm)相同,一切顺利,所以杰克逊正在做一些奇怪的事情背后场景......

{
    "fistName": "John",
    "lastName": "Smith",
    "email": "[email protected]",
    "message": "Hello"
    "accepted":true
}
jquery json spring
2个回答
4
投票

在你的jQuery ajax调用中调整你的数据值:

    $.ajax({
        type: 'POST',
        url: '/checkContact.json',
        contentType : 'application/json; charset=utf-8',
        dataType: 'json',
        data: JSON.stringify(contactForm),
        success: function(response){
            console.log(response)
            $('#success').text(response.message);
        },
        error: function(data){
            console.log(data.responseJSON.message);
        }
    })

正在发生的事情是jQuery正在将您的对象转换为查询参数字符串并发送它。这看起来像:

contactForm=%7B%22fistName%22%3A%22John%22%2C%...

杰克逊试图将查询参数解释为您的请求主体失败。

这可以通过查看浏览器中的网络选项卡并查看请求正文来确认


0
投票

如果您在控制器中手动设置ObjectMapper类,如下所示。

    ObjectMapper obj = new ObjectMapper();
    obj.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    obj.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

要么

  1. 在提供的范围中向javax.validation:validation-api添加依赖项。
  2. 将javax.validation.constraints.NotNull注释添加到您的字段中。

希望这可以帮助。

既然你已经添加了SPRING有一个标签,我会想到从后端站点设置它。

© www.soinside.com 2019 - 2024. All rights reserved.