我的userController.java:
@RestController public class UserController { private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); private final UserService userService; @Inject public UserController(final UserService userService) { this.userService = userService; } @RequestMapping(value = "/user", method = RequestMethod.GET) public List<User> listUsers() { LOGGER.debug("Received request to list all users"); return userService.getList(); } @ExceptionHandler @ResponseStatus(HttpStatus.CONFLICT) public String handleUserAlreadyExistsException(UserAlreadyExistsException e) { return e.getMessage(); } }
User.java:
@Entity public class User { @Id @NotNull @Size(max = 64) @Column(name = "id", nullable = false, updatable = false) private String id; @NotNull @Size(max = 64) @Column(name = "name", nullable = false) private String name; @NotNull @Size(max = 64) @Column(name = "firstname", nullable = false) private String firstname; @NotNull @Size(max = 64) @Column(name = "email", nullable = false) private String email; @NotNull @Size(max = 64) @Column(name = "password", nullable = false) private String password; @Transient private List<Events> events; public User() { } public User(String id, String name, String firstname, String email, String password) { super(); this.id = id; this.name = name; this.firstname = firstname; this.email = email; this.password = password; } public void setId(String id) { this.id = id; } public String getId() { return id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public List<Events> getEvents() { return events; } public void setEvents(List<Events> events) { this.events = events; } @Override public String toString() { return Objects.toStringHelper(this).add("id", id).add("name", name).add("firstname", firstname) .add("email", email).add("password", password).add("events", events).toString(); } }
有可能在我JSON的name和中重新调谐firstname
JSON
name
firstname
我的pom.xml:
<!-- Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.1.RELEASE</version> </parent> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> ...
您可以@JsonView用来控制渲染的内容。您可以在此Spring博客文章https://spring.io/blog/2014/12/02/latest- jackson-integration-improvements-in- spring中找到更多详细信息
@JsonView
假设您有View带有接口的此类Summary
View
Summary
public class View { interface Summary {} }
然后,您可以像这样注释属性:
@JsonView(View.Summary.class) private String name; @JsonView(View.Summary.class) private String firstname;
然后您的请求映射:
@JsonView(View.Summary.class) @RequestMapping(value = "/user", method = RequestMethod.GET)
这只会返回name并返回firstname结果JSON。