在发送到页面之前转换字段

发布于 2025-01-10 16:09:52 字数 785 浏览 0 评论 0原文

我有一个用户 dto 类,我需要在将其发送到前端之前转换它的一些属性。

UseDto 类

public class UserDto {
    protected Integer userId;
    protected String userName;
    protected String password;
    protected boolean enabled;
    protected boolean active;
}

现在,从我的控制器

@Override
public ResponseEntity<UserDto> getUser(Integer userId) {
    return new ResponseEntity<>(userService.findById(userId), HttpStatus.OK);
}

我得到这样的数据

{
"userId": 141,
"userName": "admin",
"password": "password",
"enabled": true,
"active": false
}

在我的例子中,在发送数据之前,我应该将布尔值(启用,活动)转换为字符串“Y”或“N”。

{
"userId": 141,
"userName": "admin",
"password": "password",
"enabled": "Y",
"active": "N"
}

我该怎么做?

I have a user dto class and i need to convert some of its properties before send it to frontend.

UsedDto class

public class UserDto {
    protected Integer userId;
    protected String userName;
    protected String password;
    protected boolean enabled;
    protected boolean active;
}

Now, from my controller

@Override
public ResponseEntity<UserDto> getUser(Integer userId) {
    return new ResponseEntity<>(userService.findById(userId), HttpStatus.OK);
}

i get data like this

{
"userId": 141,
"userName": "admin",
"password": "password",
"enabled": true,
"active": false
}

In my case, before send data, i should convert boolean values (enabled, active) to string "Y" or "N".

{
"userId": 141,
"userName": "admin",
"password": "password",
"enabled": "Y",
"active": "N"
}

How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2025-01-17 16:09:53

您可以实现自定义序列化器。看一下这个例子。

public class UserDto {
    protected Integer userId;
    protected String userName;
    protected String password;
    @JsonSerialize(using = BooleanToStringSerializer.class)
    protected boolean enabled;
    @JsonSerialize(using = BooleanToStringSerializer.class)
    protected boolean active;
}

public class BooleanToStringSerializer extends JsonSerializer<Boolean> {

    @Override
    public void serialize(Boolean tmpBool, 
                          JsonGenerator jsonGenerator, 
                          SerializerProvider serializerProvider) 
                          throws IOException, JsonProcessingException {
        jsonGenerator.writeObject(tmpBool ? "Y" : "N");
    }
}

You can implement a custom serializer. Take a look at the example.

public class UserDto {
    protected Integer userId;
    protected String userName;
    protected String password;
    @JsonSerialize(using = BooleanToStringSerializer.class)
    protected boolean enabled;
    @JsonSerialize(using = BooleanToStringSerializer.class)
    protected boolean active;
}

public class BooleanToStringSerializer extends JsonSerializer<Boolean> {

    @Override
    public void serialize(Boolean tmpBool, 
                          JsonGenerator jsonGenerator, 
                          SerializerProvider serializerProvider) 
                          throws IOException, JsonProcessingException {
        jsonGenerator.writeObject(tmpBool ? "Y" : "N");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文