Spring-boot @RequestBody DTO空值

时间:2019-07-06 09:10:19

标签: rest spring-boot dto lombok

我是Spring Boot的新手,具有以下代码:

@PostMapping(
    headers="Accept=application/json",
    consumes = { MediaType.APPLICATION_JSON_VALUE },
    produces = { MediaType.APPLICATION_JSON_VALUE  }
  )
  @ResponseStatus(HttpStatus.CREATED)
  public UserDto createUser(@RequestBody UserDto user) throws Exception {
    ModelMapper mapper = new ModelMapper();
    User createdUser = userService.createUser(mapper.map(user, User.class));
    UserDto userDto = mapper.map(createdUser, UserDto.class);
    return userDto;
  }

当我使用application / json标头发布以下JSON时,@ RequestBody用户变量正在获取空值。

{
    "username": "cmj"
}

我已经用龙目岛设置了我的DTO课程

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserDto {
    String username;
}

不确定发生了什么-任何帮助将不胜感激。

0 个答案:

没有答案