Spring boot @JsonProperty(“test”u id“在springboot@GetMapping中不起作用
我的控制器代码如下:Spring boot @JsonProperty(“test”u id“在springboot@GetMapping中不起作用,spring-boot,jackson,Spring Boot,Jackson,我的控制器代码如下: @GetMapping("/test") public TestOutputDTO getSchedule(@Valid TestInputDTO dto, BindingResult bindingResult) throws JusException { if (bindingResult.hasErrors()) { .... } ... } TestInputDTO定
@GetMapping("/test")
public TestOutputDTO getSchedule(@Valid TestInputDTO dto, BindingResult bindingResult) throws JusException {
if (bindingResult.hasErrors()) {
....
}
...
}
TestInputDTO定义如下:
@Getter
@Setter
public class TestInputDTO {
@NotNull
@JsonProperty("test_id")
private Long testId;
}
不工作,testId为空
工作
我想通过test\u id风格调用此api
我能用这个做什么
Thakns.您可以尝试以下操作:将
@RequestBody
添加到dto参数中,如下所示:
@GetMapping("/test")
public TestOutputDTO getSchedule(@Valid @RequestBody TestInputDTO dto, BindingResult bindingResult) throws JusException {
...
}
如果仍然不起作用,请编写一个单元测试来测试您的Jackson映射,看看结果如何
@Test
public void testMapping(){
TestInputDto testInput = new TestInputDto();
testInputDto.setTestId(1L);
assertEquals("{ \"test_id\" : 1}", objectMapper.writeValueAsString(testInputDto));
}
在类的顶部尝试这个
@jsonautodect(fieldVisibility=jsonautodect.Visibility.ANY)
你清理了项目吗?似乎是刷新问题。编写单元测试并将对象转换为JSON字符串。验证json字符串是否反映了test_id。您的代码看起来不错。您发送的不是json,而是一个常规请求参数。因此,使用Jackson注释(即JSON或JSON序列化)进行基本绑定显然是行不通的。@Deadpool不起作用