Spring Boot:SpringBoot 如何优雅地定制JSON响应数据返回

一、前言

  目前微服务项目中RESTful API已经是前后端对接数据格式的标配模式了,RESTful API是一种基于REST(Representational State Transfer,表述性状态转移)原则的应用程序编程接口(Application Programming Interface),它主要用于Web服务之间的数据交互。RESTful API的设计遵循一些核心原则,这些原则使得API更加简洁、灵活和可扩展。

  在构建这些API时,我们往往需要根据不同的客户端、用户角色或业务需求,灵活地控制返回的数据结构和内容。Jackson库的@JsonView注解为我们提供了一种优雅且高效的方式来实现这一目标。

二、问题场景

  @JsonView 是 Jackson 库中的一个注解,它允许你定义哪些属性应该被序列化到 JSON 中,基于不同的“视图”或“配置”。这在某些情况下非常有用,特别是当你想要为不同的用户或API端点返回不同级别的详细信息时。

  例如,你可能有一个User对象,其中包含多个属性,如id、name、email和password。当你为外部API或网站前端返回用户数据时,你可能不希望包含password字段。但是,在内部API或某些特定的情况下,你可能想要返回包含password的完整用户对象。

  这就是@JsonView可以帮助你的地方。你可以定义一个或多个“视图”类,并为你的属性指定应该出现在哪些视图中。

  在本文中,通过了解@JsonView,你将能够更好地掌握如何在Spring Boot应用中定制JSON数据的输出,从而提供更加灵活、安全且高效的RESTful API服务。

三、@JsonView主要应用场景

主要应用场景包括:

1.数据脱敏:在某些情况下,你可能不希望将数据库中的某些敏感信息(如密码、密钥等)暴露给客户端。通过使用 @JsonView,你可以定义哪些字段应该被包含在特定的视图中,并在返回数据时仅包含这些字段。

2.自定义数据输出:你可以根据客户端的需求或权限级别,定义不同的视图,并在返回数据时根据当前视图的配置来输出不同的字段组合。

四、代码实战

1. 定义实体类

package com.example.yddemo.JSONView;

import com.fasterxml.jackson.annotation.JsonView;

public class User {

    @JsonView(Views.Public.class)
    private Long id;

    @JsonView(Views.Public.class)
    private String name;

    @JsonView(Views.Internal.class)
    private String email;

    @JsonView(Views.Internal.class)
    private String password;

    public User(Long id, String name, String email, String password) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

2. 定义视图控制器

public class Views {
    public static class Public { }
    public static class Internal extends Public { }
}

3. 在代码中使用,添加@JsonView注解

package com.example.yddemo.JSONView;

import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestDemoController {

    @GetMapping("/user/Public")
    @JsonView(Views.Public.class)
    public User userPublic() {
        User user = new User(1L, "Tom", "123123@163.com", "123456") ;
        return user  ;
    }

    @GetMapping("/user/Internal")
    @JsonView(Views.Internal.class)
    public User userInternal() {
        User user = new User(1L, "Tom", "123123@163.com", "123456") ;
        return user;
    }
}

输出结果
在这里插入图片描述

在这里插入图片描述
  不想使用在Controller接口上使用@JsonView注解方式,你还可以通过编程的方式控制,通过ObjectMapper类也能方便的控制json输出。

  @GetMapping("/user/all/public")
    public String userAllPublic() {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(1L, "Tom", "123123@163.com", "123456") ;
        String publicJson;
        try {
            publicJson = mapper.writerWithView(Views.Public.class).writeValueAsString(user);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return publicJson;
    }

    @GetMapping("/user/all/internal")
    public String userAllInternal() {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(1L, "Tom", "123123@163.com", "123456") ;
        String internalJson;
        try {
            internalJson = mapper.writerWithView(Views.Internal.class).writeValueAsString(user);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return internalJson;
    }

输出结果
在这里插入图片描述

在这里插入图片描述

通过@JsonView的代码展示思考一个问题?

  如果没有该注解你会通过什么方式实现?针对不同的场景定义不同的DTO?通过@JsonIgnore注解?

  定义不同的DTO太麻烦,代码大量冗余。使用@JsonIgnore注解所有的接口都将会忽略该字段,不能精准控制单个API接口。所以整体上看还是使用@JsonView更加的灵活。

四、总结

  @JsonView 与 @JsonIgnore 区别

  ①. @JsonView 用于控制序列化时的字段组合,允许你根据不同的视图返回不同的字段集合。
  ②. @JsonIgnore 则简单地忽略某个字段,使其在序列化时不会被包含在 JSON 中。

  最后@JsonView 提供了一种灵活且强大的方式来控制序列化的 JSON 数据。通过定义视图和将它们应用到属性和控制器方法上,你可以根据需要暴露不同级别的数据,实现 API 的数据定制化。

  • 62
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中,当我们需要将String类型的数据响应JSON格式时,我们可以通过以下几个步骤来实现: 1. 首先,确保你的项目已经引入了Web和JSON的依赖。根据引用和引用所示,你需要在项目的pom.xml文件中添加对应的依赖。Web场景的依赖主要负责提供Web相关的功能,而JSON场景的依赖则用于处理JSON数据的序列化和反序列化操作。 2. 接下来,我们需要创建一个Controller类来处理String响应JSON的请求。在这个Controller类中,我们可以使用Spring Boot的注解来实现请求映射和数据响应。例如,我们可以使用`@RestController`注解来标记这个类,并使用`@RequestMapping`注解来定义请求的URL路径。 3. 在这个Controller类中,我们可以创建一个处理String响应JSON的方法。我们可以使用`@RequestMapping`或者更具体的注解(如`@GetMapping`或`@PostMapping`)来定义该方法处理的请求类型和URL路径。在方法的返回类型上,我们可以使用`ResponseEntity`类来指定响应的状态码和响应体的数据类型。对于String类型的数据,我们可以将其作为响应体,然后通过`MediaType.APPLICATION_JSON`来指定响应数据类型为JSON。 4. 最后,我们可以在方法体中返回一个String类型的数据,并确保这个数据已经被JSON序列化。这样,在客户端发起请求后,我们就可以得到一个以JSON格式封装的String数据作为响应。 综上所述,以上是在Spring Boot项目中将String类型响应JSON的一种方法。通过引入Web和JSON的依赖,并创建一个处理String响应JSON的Controller方法,我们可以轻松地实现这一功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值