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.
Related
Assuming an application with a traditional UI based on Spring MVC and Thymeleaf, it seems like there are a few ways to expose a REST API...
Add a completely separate set of Controllers using #RestController - Feels like a lot of duplication, but allows complete separation of UI vs REST logic
Use a single Controller for each entity, with a mix of both #ResponseBody methods and ModelAndView methods - Keeps all logic for a given entity in a single place, but requires mixing different concepts
Use a single Controller for each entity, use only ModelAndView, and use content negotiation with a JSON view resolver like the MappingJackson2JsonView (https://spring.io/blog/2013/06/03/content-negotiation-using-views/)
I'm particularly interested in #3 as it feels similar to how Ruby on Rails Controllers work with the respond_to for different content types.
Is this a common approach / best-practice in Spring applications?
Would POST and DELETE requests still require separate methods since they may work differently between a REST API vs the UI? (i.e. posting a form vs posting a json entity)
Would it require separate Exception handling based on whether it was a UI request or API request?
I'd go with option 1. Having a separate set of #Controller and #RestController avoids mixing MVC pattern with your REST api. To avoid code duplication, you should add a service layer and have the REST endpoint and the MVC controller to make use of that service layer.
i am new to spring and i have developed an application using spring mvc. i have created controllers which use request mapping annotations to handle mapping between jsp to controller (for example; a controller named Driver have several mappings like /drivers/update/{id}, /drivers/edit/{id} etc.). but when i use /drivers/update/{id} mapping and i want to use /drivers/edit/{id} mapping at very next call, the URL changes to "myproject/drivers/update/any_id/drivers/edit/any_id" in the URL of browser.
I think it's spring mapping handler concept related problem, but i don't know what it is actually. Please don't ask the code to be uploaded because i don't have code now. Please help(i am sure someone have already faced the problem)
I guess the URI pattern you are using is not correctly written. Prepend a forward slash / in the URI pattern.
For example:
#requestMapping("/yoururl") - correct.
#requestMapping("yoururl") - incorrect.
Note: without / it will keep appending the URL. I tried this way and it was solved in my case.
What is the advantage of using Spring view resolver.
I am using ajax calls to call the web service without view resolver. Whether changing the configuration using view resolver provides benefits.
More specifically does html view resolver provides anything related to security or session management .
Thanks in advance
No, it's not related to security or session management. In your case, i.e. when you are using #RestController or #Controller+#ResponseBody to send the response directly, view resolvers wouldn't be needed. They are needed only when you want your controller methods to forward to a view.
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"));
We have a web application that uses spring, struts and camel right now and there is a lot of customization we have done to allow us to know when beans are added to the context.
So, we have gotten to a point where we would like to remove struts from the application, because we are only using it to handle actions and we figure we could either use spring or camel to do the same thing. So I was able to get it to work with camel/velocity, but we didn't like how we really couldn't use the request object directly in the jsp (afaik, you have to put everything in the header of the Exchange and in the jsp you would do ${header.someReqVariableName}).
So we wanted to go the spring route, but since we load the context.xml directly, we have a provider that extends ContextSingletonBeanFactoryLocator and we pass the xml file name as a param, we haven't been able to figure out how to get the DispatcherServlet to work without giving it another configuration xml.
Is there a way to either:
Have camel use jsp for processing a jsp (and have all the usage of jsp tags)?
or
Have spring to see that a context has already been loaded and use that instead of having another new one?
or
Something better I have thought up?
You can use camel-jetty to expose HTTP endpoints, but I wouldn't use it for any complex web app development (JPS, etc). I'd use use Spring MVC (or similar) and use Camel for any complex routing/messaging requirements...
Here is another way, you can use the producer template to send the request to the camel context if you can get the reference of the camel context from the spring.