I am using a Spring Boot to design REST API. I want to know what difference it will make while using nested classes in request body?
REST Endpoint:
@PostMapping("/create")
public void (@RequestBody RequestData.UserDetails details) {}
My RequestData class:
public class RequestData {
public static final class UserDetails {
private String name;
}
}
So my question is which of the following class definations is safe?
- Creating a seperate class for UserDetails to get requested data
- Creating a nested class as stated above
Do nested classes affect during multiple concurrent requests or in some other way?
CodePudding user response:
Do nested classes affect during multiple concurrent requests or in some other way?
No, the request body will be mapped to an instance of the RequestData.UserDetails or simply UserDetails if it is a separate class. If you don't store this is instance as a class attribute in your Controllers, Services, etc, there is no concurrent requests concern because each request to your Controller will have its own instance of such a class and multi-threading is not a problem because you are not storing state.
This means that having a nested class or a separate class is irrelevant to the fact that you have a threadsafe Controller, Service, etc or not. The important thing is to not store state on them.
In this case, having a nested class or a separate class is more a question of taste and organization than anything else.
