我有一个简单的Spring Controller,其目的是返回我的实体对象的RESTful JSON响应。它以与我的实体匹配的标准camelCase名称返回数据。但是,我希望JSON名称与数据库字段匹配,这只是TitleCase。有一个简单的方法吗?下面是Controller和JSON返回的示例。
@Controller
@RequestMapping(path="/Users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(path="/List")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
结果JSON:
{"id":1,"userName":"SYSTEM","password":"xxxxx","firstName":"System","lastName":"System","phone":"XXX-XXX-XXXX","email":"system@test.com","reviewer":false,"admin":false}
这是我的用户实体类:
package com.prs.business;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
@Entity
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name="username")
@JsonProperty("UserName")
private String userName;
private String password;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
private String phone;
private String email;
@Column(name="isreviewer")
private boolean reviewer;
@Column(name="isadmin")
private boolean admin;
@Column(name="isactive")
private boolean active;
@Column(name="datecreated")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS")
private Timestamp dateCreated;
public User() {
userName = "";
password = "";
}
public User(int id, String userName, String password, String firstName, String lastName, String phoneNumber,
String email, boolean reviewer, boolean admin) {
super();
this.id = id;
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.phone = phoneNumber;
this.email = email;
this.reviewer = reviewer;
this.admin = admin;
}
public User(String un, String pw, String fn, String ln, String pn, String e, boolean m, boolean a) {
setUserName(un);
setPassword(pw);
setFirstName(fn);
setLastName(ln);
setPhoneNumber(pn);
setEmail(e);
setReviewer(m);
setAdmin(a);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhone() {
return phone;
}
public void setPhoneNumber(String phoneNumber) {
this.phone = phoneNumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isReviewer() {
return reviewer;
}
public void setReviewer(boolean inReviewer) {
this.reviewer = inReviewer;
}
public boolean isAdmin() {
return admin;
}
public void setAdmin(boolean inAdmin) {
this.admin = inAdmin;
}
public boolean isActive() {
return admin;
}
public void setActive(boolean inActive) {
this.admin = inActive;
}
public Timestamp getDateCreated() {
return dateCreated;
}
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", firstName=" + firstName
+ ", lastName=" + lastName + ", phoneNumber=" + phone + ", email=" + email + ", reviewer="
+ reviewer + ", admin=" + admin + "]";
}
}
答案 0 :(得分:0)
userRepository.findAll()可能会返回一个POJO,对吗? 在那个pojo中,在getter之前或字段声明之前,你可以添加一个像
这样的注释@JsonProperty("<hereYouWriteTheNameYouPrefer>")
private String userName;
或
private String userName;
@JsonProperty("<hereYouWriteTheNameYouPrefer>")
public String getUserName(){
return userName;
}
答案 1 :(得分:0)
在application.properties
spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE