Spring boot pre api action annotation - validation

I'm new using Spring boot , but i come from a Play! framework background.
In Play there was something called action "Action Composition"
https://www.playframework.com/documentation/2.6.x/JavaActionsComposition
It is used to do some code logic before going through the api method , so it could be used for creating a custom security for example or whatever logic we want to add before accessing an api method.
Then we simply annotate whatever api with this Action.
Is there something similar to do in Spring boot ?
I have googled for actions/ validators , but i did not find results similar to Play framework's action composition behavior.

What you are looking for is called an interceptor. Its purpose is to allow some action to be taken before an actual API endpoint is reached.
This documentation should help.

Related

Spring : Auto Generate CRUD Rest Controller

Is there a way to generate spring rest crontroller for a business flow.
I want to generate CRUD services for given database object.For example, "employee_mst" , generate CRUD services code automatically.This is similar to what we used to have in hibernate or what we have in loopback.io for node. Appreciate any help around it.
I found a link that may answer your question: https://docs.spring.io/spring-data/rest/docs/current/reference/html/.
This text explains that Spring Data REST generates REST interfaces from Spring Data repositories.
I intend to try this but did not do so yet.
EDIT: I saw in my example application that Spring Data REST did what I expected. I could request all entities in my Spring Data repository using a HTTP request. The returned JSON contained also discovery information. You may prefer writing your own controller to have more control on what information is returned.

A layer before controller - spring mvc

I have a spring mvc application. Now in my project, I basically want to execute the latest request and all the previous requests are cancelled/terminated.
Currently all the cancellation code is inside my controller. But for code clarity/readability I want to port this code outside my controller. Ultimately I want a layer above, which takes requests, checks if the task is to be cancelled or not and only then forward it to the controller.
One way of doing above would be to have another controller which forwards it to the above controller. I have also heard of Handler Interceptor. What is the best way of doing it?
I would go with an interceptor if I were you.
you can check this link for examples on how to implement you own

Seems like struts validate method doesn't work anymore when using spring

I am now moving to Struts for my presentation layer. I have done a simple example using Struts alone (not really, actually, I test an example inside a simple maven project). From now on, I am likely to use Struts with Spring so I try to migrate my simple application, my goal was to use service layer to deal with specific operation.
In my simple test, I extend action from Struts action, now that I am using Spring, I extend it from ActionSupport so that I can fully use spring injection. Now it seems like even though I override validate method, it is no longer called. Is that the right behaviour, if so, where should perform operations like checking if my mandatory fields are populated (inside action or service ?)
Thanks for your answer !

Spring Context Event

I am currently studying Spring.
While reading a Spring book, I met a part regarding Event.
By using context.publishEvent(..), I could trigger the event.
But I don't know what It's exactly for.
I can use other Method instead of using complicated publishEvent.
Please, tell me. thank you.
Spring Events are used to implement publish-subscribe model (or observer pattern) where two not-related parts of code must be somehow connected.
Think of the analogy of web applications where servlet container creates http sessions and your code is informed about this using javax.servlet.http.HttpSessionListener.
Spring uses this mechanism internally. It's much more visible in Spring Security where several parts of the code are informed about e.g., successfull authentication.

Session handling in Struts 2.1.6

I have a project with the following setup:
Tomcat 6.x
Struts 2.1.6
DisplayTag 1.2
Spring 2.x (1 or 5, don't remember now)
I want to know to to do session controlling in every action of my app, like if the users weren't logged in, they're redirect to certain page to login (in the case of my project, either the user come to a special crafted url like login/SPECIALHASHTOLOGIN or won't enter at all.
Need more details?
Thx in advance.
I'm still new to S2 as well, but I believe what you will need to do is modify the default interceptor stack (or create a custom stack) and add a custom interceptor. This custom interceptor will need to implement SessionAware to access the user session, and must implement your custom logic (which action to redirect to, which URLs do not need protection, etc.).
Here is a good tutorial of a LoginInterceptor that behaves similar to what you are requesting.
Acegi security is a great way to add security to your web app if you're already using Spring. Here's a decent 1-hour Acegi tutorial.

Resources