env:spring boot 2.0,杰克逊序列化
我的rest控制器方法不会返回任何主体,就像ResponseEntity.ok().build()
一样,但是ajax不会进入成功方法,它将进入错误状态,并且错误显示“ JSON输入意外结束”
我尝试过:
1.将属性添加到application.properties中:spring.jackson.default-property-inclusion: NON_NULL
,仍然转到错误方法;
2.像这样ResponseEntity.ok("{}")
更新返回结果,就可以了;
@PostMapping(value = "/import")
public ResponseEntity<Void> importRecipients(@ModelAttribute UserRecipientDto userRecipientDto,
@CurrentUser @ApiIgnore User user,
@RequestParam ImportType importType,
@RequestParam String name,
@RequestParam List<Long> ids) {
Collection<MailRecipient> count = this.service.getRecipients(userRecipientDto, importType, ids);
UserMailRecipientGroup userMailRecipientGroup = new UserMailRecipientGroup(name, USER_RECIPIENT, count, user);
userMailRecipientGroupService.saveMailRecipientGroup(userMailRecipientGroup);
return ResponseEntity.ok().build();
}
application.yml:
spring:
jackson:
serialization:
write-dates-as-timestamps: true
write_dates_with_zone_id: true
time-zone: GMT
default-property-inclusion: NON_NULL
ajax代码块:
url: url,
type: type,
contentType: "application/json;charset=utf-8",
headers: {},
data: param,
async: true,
dataType: "json",
success: function success(data) {
cb(data);
},
error: function error(request, status_code, text_error) {}
我希望这样:
当我的rest方法返回ok().build()
时,ajax将转到成功方法。
我猜ajax将结果转换为json会发生一些错误,可能是某些spring boot属性可以做到的,但是我还没有找到。
谢谢!
答案 0 :(得分:0)
答案 1 :(得分:0)
从返回ok()。build()中,您只是返回Http Status 200 OK Status。不是吗?
您到底要返回给客户什么?
如果有一些回应,您可以寻求 返回ResponseEntity.ok()。build() 或
return Response.ok()。entity(someEntity).build();
我从代码中看到返回类型是ResponseEntity。
请说明问题所在。
编辑:
代码类似于
@PostMapping(value =“ / import”)
public ResponseEntity<String> importRecipients(@ModelAttribute UserRecipientDto userRecipientDto,
@CurrentUser @ApiIgnore User user, @RequestParam ImportType
importType, @RequestParam String name,
@RequestParam List ids) {
Collection count = this.service.getRecipients(userRecipientDto, importType, ids);
UserMailRecipientGroup userMailRecipientGroup = new UserMailRecipientGroup(name, USER_RECIPIENT, count, user);
userMailRecipientGroupService.saveMailRecipientGroup(userMailRecipientGroup);
return new ResponseEntity(HttpStatus.OK);
}
答案 2 :(得分:0)
这是一个多么愚蠢的问题,我不应该怀疑我的控制人。实际上,问题发生在前端,我的同事支持一个错误的json解析器!!