Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring rest controller having a request body with generated default value?

I can have request on my rest controller with a payload such as

{
"id" : "dffds-fsdf-dfdf-dsf",
"name" : "toto",
"age" : "18"
}

But the body param id is not mandatory and could be null. In this case I need to generate an ID (Using UUID or anything else)

Is there a way to do that using annotation for instance ?

like image 459
Seb Avatar asked Sep 09 '26 13:09

Seb


2 Answers

One way you can do this just assigning default value to variable, so if the body param id is null then this default value will be assigned, but if id is passed in body this default value will not be assigned.

public class Model {

private String name;

private String id=UUID.randomUUID().toString();

private int age;

}

like image 142
Deadpool Avatar answered Sep 11 '26 05:09

Deadpool


We have to address at least two cases:

  1. the id field is sent as null, e.g. { "id": null, ... }
  2. the id field is not present in the payload, e.g. { "name" : "toto", "age" : "18" }

It seems that there isn't an annotation that can produce this behaviour, but the following payload-mapping class should do the trick:

import java.util.UUID;

public class Payload {

    private String id = UUID.randomUUID().toString();
    private String name;
    private String age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id != null ? id : UUID.randomUUID().toString();
    }

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    /* You can safely remove this method, it's here only for test purposes. */
    public static void main(String... args) throws java.io.IOException {
        String[] tests = new String[] { 
                "{ \"id\": \"dffds-fsdf-dfdf-dsf\", \"name\": \"toto\", \"age\": \"18\" }", 
                "{ \"name\": \"toto\", \"age\": \"18\" }", 
                "{ \"age\": \"18\" }", 
                "{ \"id\": null, \"name\": \"toto\", \"age\": \"18\" }",
                "{ \"id\": null, \"age\": \"18\" }", 
                "{ \"id\": null, \"name\": \"toto\" }", 
                "{ \"id\": null }", 
                "{ }" 
            };

        for (String it : tests) {
            Payload payload = new com.fasterxml.jackson.databind.ObjectMapper().readValue(it, Payload.class);
            System.out.println(it + " => [id=" + payload.getId() + ", name=" + payload.getName() + ", age=" + payload.getAge() + "]");
        }
    }
}

If you run the main method, you'll obtain an output like the following one, which should meet your requirements:

{ "id": "dffds-fsdf-dfdf-dsf", "name": "toto", "age": "18" } => [id=dffds-fsdf-dfdf-dsf, name=toto, age=18]
{ "name": "toto", "age": "18" } => [id=f3218a7c-6e2c-47fc-93b9-746ceec7b56b, name=toto, age=18]
{ "age": "18" } => [id=21899f52-d273-4c89-8a16-26871f4ec351, name=null, age=18]
{ "id": null, "name": "toto", "age": "18" } => [id=0a2ba8f6-cee3-44f0-89a6-e18ac55346d2, name=toto, age=18]
{ "id": null, "age": "18" } => [id=4f8125eb-ec68-4343-a04f-1490ffb81a76, name=null, age=18]
{ "id": null, "name": "toto" } => [id=b5d59feb-5730-4929-bc41-b0f17da68a39, name=toto, age=null]
{ "id": null } => [id=ccb9b192-daa3-4877-b232-56ec483b9d8e, name=null, age=null]
{ } => [id=d56cc66c-f9af-4dea-8ad4-86c16c9921a7, name=null, age=null]
like image 27
Robert Hume Avatar answered Sep 11 '26 04:09

Robert Hume