Skip to content

How to make a custom message from an already defined response on Spring

My API is returning me this:

[
    {
        "id": 1,
        "uuid": "@B7304",
        "username": "blabla",
        "hearthBeat": 30,
        "status": "well",
        "date": "13/05/1333",
        "latitute": 30,
        "longitude": 40,
        "cardiacSteps": 50
    },
    {
        "id": 2,
        "uuid": "@B7304",
        "username": "blabla",
        "hearthBeat": null,
        "status": null,
        "date": null,
        "latitute": null,
        "longitude": null,
        "cardiacSteps": null
    }
]

The problem is, I would like to, on the array that is represented by the second ID, return a error message, as there is no data in it. Something like this:

[
    {
        "id": 1,
        "uuid": "@B7304",
        "username": "blabla",
        "hearthBeat": 30,
        "status": "well",
        "date": "13/05/1333",
        "latitute": 30,
        "longitude": 40,
        "cardiacSteps": 50
    },
    {
        "uuid": @B7304,
        "message": "This user has no data"
    }
]

My code is as follows:

    @GetMapping("/monitored")
    @PreAuthorize("hasRole('USER') and hasRole('RESPONSIBLE')")
    public Object returnMonitored() {

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        Optional<User> username = userRepository.findByUsername(auth.getName());

        List<Dependency> uuids = dependencyRepository.returnAllUserUuid(Objects.requireNonNull(username.orElse(null)).getUuid());
        List<Health> health = new ArrayList<>();

        uuids.forEach(uuid -> {
                    if (userRepository.existsByUuid(uuid.getUserUuid()) && healthRepository.existsByUuid(uuid.getUserUuid())) {

                        if(healthRepository.existsByUuid(uuid.getUserUuid()) && healthRepository.isRegistered(uuid.getUserUuid())) {
                            List<Health> healthList = healthRepository.returnAllUserUuid(uuid.getUserUuid());
                            health.addAll(healthList);
                        }
                    }
                }
        );

        if(health == null || health.isEmpty()) {
            return ResponseEntity.ok().body(new MessageVo("You're not responsible for any user ;) "));
        }

        return health;
    }

With this, I can’t seem to override the specific response, as it is an List of the Entity(Health).

Thanks for your help!

Answer

What you want to do is a really bad idea since one would expect all elements in a JSON array to have the same attributes.

You should return what you are returning now with all the nulls and return your “message” in status.

[
    {
        "id": 1,
        "uuid": "@B7304",
        "username": "blabla",
        "hearthBeat": 30,
        "status": "well",
        "date": "13/05/1333",
        "latitute": 30,
        "longitude": 40,
        "cardiacSteps": 50
    },
    {
        "id": 2,
        "uuid": "@B7304",
        "username": "blabla",
        "hearthBeat": null,
        "status": "This user has no data",
        "date": null,
        "latitute": null,
        "longitude": null,
        "cardiacSteps": null
    }
]

I would also recommend adding a “statusCode” attribute where you can return a numerical code and have “status” represent the statusCode description since doing string compares on status isn’t a good idea either.