Javascript 从HTML表单获取spring启动REST请求参数中的null

Javascript 从HTML表单获取spring启动REST请求参数中的null,javascript,java,jquery,spring-boot,Javascript,Java,Jquery,Spring Boot,我有一个HTML表单,其中有2个输入,提交给spring bootRESTapi。但在这个简单的应用程序中,我仍然在后端api中接收null作为请求 表格 <div> <label>alphaID</label> <div> <input id="alphaID" name="alphaID" type="text"/> </div> </div> <div&g

我有一个HTML表单,其中有2个输入,提交给spring boot
REST
api。但在这个简单的应用程序中,我仍然在后端api中接收
null
作为请求

表格

<div>
    <label>alphaID</label>  
    <div>
        <input id="alphaID" name="alphaID" type="text"/>
    </div>
</div>

<div>
    <label class="col-md-4 control-label">Domain Name</label>  
    <div class="col-md-4">
        <input id="domain" name="domain" type="text"/>
    </div>
</div>
后端是一个带有REST调用的spring引导应用程序:

@RequestMapping(value = "/validate", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public Map<String, List<String>> validate(@Valid MyDTO myDTO) {

    System.out.println(myDTO.getId());          // GETTING null
    System.out.println(myDTO.getDomain());      // GETTING null

}

查看您的
requestJson
是否实际具有正确的格式,以便
MyDTO
使用它,这可能很有趣


您也不必
Json.stringify
您的数据。执行此操作时,基本上只需向后端发送一个字符串。后端不知道必须解析此字符串才能获得有效文档。您可以直接在data属性中发送JavaScript对象,也可以更改API以获得字符串,然后在函数中对其进行解析。

内容类型更改为

consumes = MediaType.APPLICATION_JSON_VALUE
添加
@RequestBody
注释

公共映射验证(@Valid@RequestBody MyDTO MyDTO)

确保您正在调用正确的URL并从浏览器请求中发送正确的
内容类型。

从浏览器中,我应该发送
应用程序/json
,对吗?我做了更改,我得到错误:
已解决[org.springframework.web.HttpMediaTypeNotSupportedException:Content-type'application/x-www-form-urlencoded;charset=UTF-8'不受支持]
请参阅本文,在Ajax调用中添加
内容类型
接受
标题。
public class MyDTO {
    @JsonProperty("alpha_id")
    private String alphaID;

    @JsonProperty("domain")
    private String domain;

    ....
}
consumes = MediaType.APPLICATION_JSON_VALUE