0

I have been confused due to the different usages and annotations for request & response class in Java. Some of them use @Value, some others @Data. Similarly some of them @RequiredArgsConstructor, some others @AllArgsConstructor. So, could you pleae post a suitable request and response classes (for example 'EmployeeRequestandEmployeeDTO`) for a general Java convention?

Request:

@Data
public class BrandRequest {
    @NotEmpty
    private String name;
    private UUID cityUuid;
}

Response (DTO):

@Data
@NoArgsConstructor
public class BrandDTO {
    private UUID uuid;
    private String name;

    public BrandDTO(final Brand brand) {
        this.uuid = brand.getUuid();
        this.name = brand.getName();
    }
}
  • 3
    `@RequiredArgsConstructor` and `@AllArgsConstructor` and `@Data` are annotations for Lambook and are utils to not write more code in your class, in spring boot you can use `@RequestBody` or `@ResponseBody` [check ]( https://www.baeldung.com/spring-request-response-body) – Abder KRIMA Dec 10 '21 at 13:55
  • https://spring.io/guides/tutorials/rest/ – ⵔⴰⴼⵉⵇ ⴱⵓⵖⴰⵏⵉ Dec 10 '21 at 14:03
  • @TinyOS Thanks, but I know that. I just need a best practices for Request, DTO and response classes. –  Dec 11 '21 at 22:40
  • @TinyOS Could you please have a look at my update (added request and response/DTO classes that I am using in the project). So, it there a better approach or notation examples? I also have a look at Spring Boot project on GitHub, but not found a best practices or similar examples in there. Any idea? –  Dec 14 '21 at 07:26
  • @ⵔⴰⴼⵉⵇ ⴱⵓⵖⴰⵏⵉ Could you please have a look at my update (added request and response/DTO classes that I am using in the project). So, it there a better approach or notation examples? I also have a look at Spring Boot project on GitHub, but not found a best practices or similar examples in there. Any idea? –  Dec 14 '21 at 07:27
  • `@Value` helps you to get a value that you need from `properties` or `yaml` file. If you go the `@Data` you will find that it contains `getter`, `setter`, `toString` and other annotations, means it helps to not write all these methods in your java class. Same for `...Contructor` annotations. voila! maybe you can explain more your need please – Abder KRIMA Dec 14 '21 at 13:18
  • Do you mean that for Request and DTO class above, it is good to use `@Data` rather than `@Value`. Is that true? –  Dec 14 '21 at 15:20

0 Answers0