Home > Software design >  Convert fields before sending to the page
Convert fields before sending to the page

Time:03-01

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?

CodePudding user response:

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");
    }
}

CodePudding user response:

This probably isn't the answer you're looking for, but...

UserDto isn't really a DTO. It looks like it's an entity class. The DTO would be the class used to perform crud operations on the entity.

In the example below, the dto fetches the entities:

public interface UserDto {
   public List<UserEntity> fetchUserEntities()
}

It sounds like you might need to restructure your project. You should think about the application as a tiered system, with presentation, business, and data layers. Spring calls these layers: Controllers, Services, and Repositories

The Controller represents the presentation layer. Its responsibility is to perform only the ditties related to managing communication with the client. It should really only ever call Services.

Services represent the business layer. This is where the meat of your application lives. This layer would be responsible for converting your entities into the domain objects returned to the controller. In this layer, when converting from your UserEntity to something like UserDTO you'd do your conversion there. You could return a modified instance of UserEntity, though it might bite you later.

Repositories are your DTOs, Clients that talk to external services, or anything that pulls data.

  • Related