在我的Json

时间:2016-03-11 13:49:37

标签: json spring-boot

我的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();
    }
}

可以在JSONname

中重新调整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>
...

1 个答案:

答案 0 :(得分:3)

您可以使用@JsonView来控制渲染的内容。您可以在本春季博文https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring

中找到更多详细信息

假设您有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)

这只会在生成的JSON中返回namefirstname