spring中bean转json时首字母、第二个字母是大写变小写的问题

@GetMapping(value = "user/{id}")
    public User getUser(@PathVariable("id")Long id){
        User user = new User();
        user.setId(1l);
        user.setUPhone("12121212");
        user.setUserName("用户名");

        return user;
    }

实体类

import lombok.Data;
@Data
public class User {

    private Long id;

    private String UserName;

    private String uPhone;
}

接口返回数据

{
    "id": 1,
    "userName": "用户名",
    "uphone": "12121212"
}

在接口的返回数据中UserName变成了userName,uPhone变成了uphone。

两个都解决了

  @JsonProperty("UserName")
    private String userName;

    @JsonProperty("uPhone")
    private String uPhone;

  <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.9</version>
        </dependency>

解决原理是:在转json时字段变了,那就让这个字段在转成json的时候按照 @JsonProperty(“uPhone”)上的字段来生成即可。

对于uPhone只需要重写它的get和set方法即可

 public String getuPhone() {
        return uPhone;
 }

 public void setuPhone(String uPhone) {
      	this.uPhone = uPhone;
  }
发布了30 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/syr1136877833/article/details/90644530