URL keep appending in spring mvc application - spring

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.

Related

Spring boot rest best practices

this is the first time I am creating restful services using spring Boot. I am little bit confused about best url practice should I use the following ?
#DeleteMapping ("/cars/delete/{id}")
Or
#DeleteMapping ("/cars/{id}")
Because I will already be having a get mapping for ("/cars/{id}") but annotation is different as I will be using this
#GetMapping ("/cars/{id}")
While for delete or put mapping I will be using their respective annotations
So can I use same path with different mapping or I should use different paths too with different mappings ?
Restful convention says you should design your urls to be resource related and use HTTP methods as the verbs. The best practice in this case would be to use
#DeleteMapping("/cars/{id}")
You should not worry about your consumers accidentally calling the wrong method. This is well known to be the best practice as restful urls do not contain verbs like delete.

spring MVC forwarding request to another controller

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.

Spring Data Rest modify Repository method URI

This may be a bit of a rudimentary question, but I have a repository where I can do a find by username as follows:
....../items/search/byUsername/?username=myusername
However, this is generally inconsistent with how AngularJS Resources treat queries. Is there a way to instead make the request URI for the same thing to be
....../items/?username=myusername
Or does that functionality not exist with spring data rest? custom methods can be made in the angular resource but it is tedious to do that for every possible search category
If Angular (read: a client library) defines what URI's have to look like, then it's fundamentally violating a core REST principle — which is that clients follow links and URI templates.
That said, if you're using Querydsl and let your repository extend QuerydslPredicateExecutor, collection resources can be filtered by using property expressions. See the reference documentation for details.
Another option would be to manually expose resources under the URIs expected and manually forward the calls. But again, I'd argue you're better off teaching Angular to behave like a proper REST client in the first place 🙃.

Spring MVC Identifying the Controller used for a given url/view

I am working on a well undocumented project and I was wondering if is it possible to get the controller used for a certain url or view?
Without knowing more information, one of the easiest ways is to turn on DEBUG logging for the Spring servlet. For example if you are using log4j:
log4j.logger.org.springframework.web.servlet=DEBUG
After you turn that on, you'll see log entries anytime a page is hit like this:
Mapping [/some/path] to HandlerExecutionChain with handler [com.myapp.controller.MyController#4cda661a]
Anyway, there are various ways to do this, including scanning the code for the view name or path. Another approach would be to create a interceptor that logs some additional information for every request.
You could try Spring Tool Suite (comes either standalone or as an add-on for Eclipse). It's essentially Eclipse with extra Spring-specific features, one of them is what you're looking for:

A heavily customized Spring Web application and the dispatcher servlet

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.

Resources