Camel-case fields are not recognized by API.

huangapple go评论77阅读模式
英文:

Why camel-case fields are not recognized by API?

问题

我已经定义了我的请求类如下:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SampleRequest {

    @NotBlank
    private String firstName;
    
    private String middleName;

    @NotBlank
    private String lastName;
}

Controller 如下:

@PostMapping(path = "mydetails", consumes = {MediaType.APPLICATION_JSON_VALUE})
public DeferredResult<DetailsResponse> saveMyDetails(@Validated @RequestBody SampleRequest sampleRequest) {

    // 这里有一些逻辑

    return detailsResponse;
}

当我从Postman发送我的请求时:

{
   "firstName":"John",
   "middleName":"For",
   "lastName":"Doe"
}

在控制器中接收到的所有字段的值都为null。经过调查,我发现它以某种方式不接受驼峰命名法的字段,而是接受带下划线的字段。所以,我尝试以以下格式调用API,它起作用了:

{
   "first_name":"John",
   "middle_name":"For",
   "last_name":"Doe"
}

为什么会这样?我们在请求类中定义的方式应该与从Postman或应用程序端发送请求时保持一致。为什么只有在必须发送带下划线而不是驼峰命名法时才起作用?

英文:

I have defined my request class like:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SampleRequest {

    @NotBlank
    private String firstName;
    
    private String middleName;

    @NotBlank
    private String lastName;
}

Controller as:

    @PostMapping(path = &quot;mydetails&quot;,  consumes = {MediaType.APPLICATION_JSON_VALUE})
    public DeferredResult&lt;DetailsResponse&gt; saveMyDetails(@Validated @RequestBody SampleRequest sampleRequest) {

    	// some logic here

        return detailsResponse;
    }

When I'm sending my request from Postman as:

{
   &quot;firstName&quot;:&quot;John&quot;,
   &quot;middleName&quot;:&quot;For&quot;,
   &quot;lastName&quot;:&quot;Doe&quot;,
}

the value of all fields received at controller is null. Upon investigating, I found that it is somehow not accepting the fields in camel-case, rather it is accepting with underscore. So, I tried hitting the API with below format and it worked:

{
   &quot;first_name&quot;:&quot;John&quot;,
   &quot;middle_name&quot;:&quot;For&quot;,
   &quot;last_name&quot;:&quot;Doe&quot;,
}

So, why it is like this? Whatever fashion we defined in our request class, it should be same while sending the request from Postman or from app side. Why is it only working when I have to send with underscore instead of camel-case?

答案1

得分: 0

尝试使用以下方式为你的类(SampleRequest)添加注释:

@JsonNaming(PropertyNamingStrategies.LowerCamelCaseStrategy.class)
英文:

Try to annotate your class (SampleRequest) with:

@JsonNaming(PropertyNamingStrategies.LowerCamelCaseStrategy.class)

答案2

得分: 0

请参照以下方式在您的请求类中使用任何您想要的字段:

@JsonProperty("firstName")
private String firstName;

@JsonProperty("middleName")
private String middleName;

@JsonProperty("lastName")
private String lastName;
英文:

Use like this for any field you want to in your request class:

@JsonProperty(&quot;firstName&quot;)
private string firstName;

@JsonProperty(&quot;middleName&quot;)
private string middleName;

@JsonProperty(&quot;lastName&quot;)
private string lastName;

huangapple
  • 本文由 发表于 2023年2月27日 17:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定