在发送到页面之前转换字段
我有一个用户 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现自定义序列化器。看一下这个例子。
You can implement a custom serializer. Take a look at the example.