Spring boot Bean concept in Rest API - spring

while creating rest api in spring boot , at database end we pass the object of entities to the methods , i dont want to pass the entity i want pass the bean
Please explain anyone how to create the bean and pass it to the all methods

Related

Standard Scope for Spring classes

In a spring MVC app , by default all beans are singleton ,but what should be the standard scopes for below classes according to good programming practices:
1.DAO classes
2.Controller classes
3.DTO classes
4.Service classes
I have read that DAO and Controller classes should be singleton scoped and DTO classes should not be beans so not annotated, whenever required, DTO classes should be instantiated using "new".
What will be the scope of #Service classes ?
And Which classes will have the Request and Session scopes if none of the above classes are created in these 2 scopes?
First of all not classes, but Spring managed beans have a scope. Difference is that you can have classes in your application that you didn't configure to be managed by Spring (So for example you didn't provide #Component annotation)
For the Spring managed beans default scope is Singleton. That means Spring container will provide the same instance everytime you ask for that bean to be autowired.
You can change that default scope with for example #Scopeannotation. So to answer your question, all of the above mentioned choices would have default scope of singleton but you could changed that to be requestor sessionscope if you would like (only applicable in web applications though). You can read more about setting scopes here.
ps. DTO classes are usually not declared to be managed by Spring - letting Spring manage a simple data transfer object doesn't make much sense.
So basically two things to consider here. The 1st is that if a bean is required to be declared as a spring bean . It depends on if you need to use the spring features for this class such as #Transactional , #Async , #PreAuthorize , #Autowired (i.e dependency injection) , or ensure the bean has certain scope etc. If not , it is simpler not define it as a spring bean and simply create it by yourself.
So the following types of the classes are required to define them as spring bean in most cases:
DAO because most probably need to inject EntityManager or JdbcTemplate to it
Controller because it is a part of spring-mvc and you need to define it as a bean such that you can use #RequestMapping / #GetMapping / #PostMapping / #PutMapping / #DeletMapping / #PatchMapping etc. on its method.
Service class because you need to inject it into the controller and you need to use #Transactional to manage the DB transaction for its method.
For DTO , in most case you can create it by yourself since it is just a data container in nature and does not require to use any spring features.
The 2nd thing to consider is what scope does a bean should be. You mainly need to think about if an instance of that class is okay to be executed safely by multiple request (i.e thread) concurrently. If yes , you can simply use the default singleton scope. If not , you can think about if you want each HTTP request (i.e #RequestScope) or each HTTP session (i.e. #SessionScope) has their own instance of that class to work with. For example , if you are implementing some shopping cart , you most probably want that the HTTP session has their won instance of a shopping cart and so you should use #SessionScope for the shopping cart.

Spring Boot / MVC - How to customize handling of return types for controller methods?

Spring Boot 2.3
I would like to return Kotlin's Result type from a Spring Boot controller method. This type can either be an exception or a success. If it is an exception, I would like to extract this and allow it to flow to the normal ExceptionHandler. If it is a success, then I want to extract this and allow it to flow out as if it had been returned from the controller method.
I am having trouble finding information on how to add additional controller result type handlers in Spring Boot. Any help that can be provided toward this would be much appreciated.

Spring Boot/JPA not persisting with service layer

I'm using Spring boot along with Spring Data JPA.
I'm stucking in an odd scenario when saving a new entity.
Unsing method from extended CrudRepository class, all works as expected.
If I inject via #Autowired the CrudRepository interface in my service layer, the method still works, but nothing is persisted on db.
The returned object from 'save' method seems ok, since I get an always increasing ID value.
Suggestions?
Cheers
FB
check whether we are populating the data properly in your bean and check it before passing to save method of spring data jpa

Best way for validate Bean in Spring+Jersey rest api

I want to validate bean in rest api. In Spring mvc application we can use BindingResult for validate bean. But when We make rest application in spring , i am not able validate bean with the help BindingResult. I knew that we can validate bean in rest api using The JSR 303 . But my want to know what is best practice for validate a bean in spring rest application and this bean created in application.

Issue in understanding the Spring Bean Scopes Vs Spring web flow Session Scopes

We Know the Spring Framework gives
singleton,
prototype,
request,
session,
global_session
bean Scopes.
Also we know that Spring web flow gives flowScope,viewScope,requestScope,flashScope,conversationScope.
So If i mentioned one Component, say Student, as #Component #Scope=singleton in a spring MVC project. For each request, will it create a new Student Object or Spring container will create only once?
You are confusing yourself with objects and beans.
For each request, will it create a new Student Object or Spring container will create only once?
The functioning of Spring is purely using beans. When you declare a something like a #Component, it's just an annotation that tells Spring that the part you've declared as a component is either Model or View or Controller i.e. a component of MVC. When you say something like #Scope=singleton, it tells Spring that only a single object instance can access the bean.
Let me make it more clear. Say you and I are objects and a strawberry candy is a bean. So if you have the candy. I cannot take it from you. Meaning only one of us can have that candy. It's the same thing with singleton scope.
Hope I made things simpler.. :)

Resources