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
Related
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.
I have few spring controllers all of these controller modelAttribtes are extended some commonForm(BaseForm). All common properties were in BaseForm and specific properties are in sub classes which acts as ModelAttributes for controllers.
Based on special condition I have scenarios to forward to another controller but while this forward is happening the request contains old data as well and giving double values to the parameters and failing the forwarded request.
Actually this code is copied from struts based project as part of migrating to spring MVC.
Please help me on this.
Thanks,
Syamala.
Is there any way of testing the functionality of a forward:/ view being returned from a Spring MVC controller in a JUnit test?
I'm using the MockMvc functionality from Spring 3.2 and my controller under certain circumstances forwards to another (by means of returning a view name of forward:/pathHandledByController).
It'd be great to be able to assert that when this forwarding has taken place, all the #ModelAttributes from the second controller are applied and everything processes properly. Unfortunately MockMvc only lets me assert that the view name returned started with forward:/.
Is there any way I can test this without spinning up the whole web-app in something like Jetty? I've got lots of services plumbed into the MVC application, how would I create a web-app that uses separate Spring config (from src/test/resources) with mocks of these services?
You can use forwardedUrl matcher:
mockMvc.perform(get("/controller/path"))
.andExpect(forwardedUrl("/forwarded/url"));
I'm new to java and web apps and after trying out a few things I went with a set up of Spring webmvc using annotations and velocity as templating engine. It's not that hard to do simple #RequestMapping annotations to controller methods and returning ModelAndView instances filled with data, however I was wondering how things are done when you have data you need in the model that occurs on every page, for example "latest 5 news items" or something similar. You could of course always fill the model with such data in every method that is handled by a #RequestMapping, but I'm quite sure that that is not the way to do it.
What is the correct way of filling the model with recurring data, without poluting your controller methods with calls to the same method for this recurring data.
Any help is appreciated.
You could use a servlet filter or a Spring interceptor, and get the recurring data from this filter or interceptor and place it in a request attribute.
Another solution is to let the page call more than one controller, for example by using multiple ajax requests. Then one controller could is responsible for the specific page and another is responsible for "latest 5 news items", see related question.
Good question dude. In my current app i am using sessions to store my username which is appearing on all the rest of the app.
#JB Nizet thanks for the link.. now ill go for spring interceptor
In it's simplest form, I want an interceptor that checks session data to see if a user is logged in, and if not redirects them to the login page. Obviously, I wouldn't want this interceptor to be used on say the welcome page or the login page itself.
I've seen a design that uses a listing of every url to one of two interceptors, one doing nothing and the other being the actual interceptor you want implemented, but this design seems very clunky and limits the ease of extensibility of the application. It makes sense to me that there should be an annotation-based way of using interceptors, but this doesn't seem to exist.
My friend has the idea of actually modifying the handler class so that during each request it checks the Controller it is mapping the request to for a new annotation we would create (ex #Interceptor("loginInterceptor") ).
A major point of my thinking is the extensibility, because I'd like to later implement similar interceptors for role-based authentication and/or administration authentication.
Does it sound like my friend's approach would work for this? Or what is a proper way of going about doing this?
Use Spring Security.
Please have a look at these sites, Spring Framework Annotation-based Controller Interceptor Configuration and
Ability to restrict HandlerInterceptors to specific controller paths
Hope it will be useful.
What about a Servlet Filter on all requests that sends the user to the login page if the user object isn't in the session? For the second part you can use security annotations on the controller methods that can check the user's role.