Spring boot rest best practices - spring

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.

Related

URL keep appending in spring mvc application

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.

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 🙃.

Multiple or single controller in Spring MVC?

What is the ratio in defining multiple controllers (in Spring MVC) instead of just one that matches all paths?
Could you write me also a use case scenario?
Following the suggestion of #Lyju, the number of Controller in a Spring MVC application is a personal choice, but it is better to follow the single responsability principle: every module should have responsibility over a single part of the functionality provided by the software.
For furhter details:
https://en.wikipedia.org/wiki/Single_responsibility_principle

What's the best practice of Spring MVC and ORM lazy loading?

I've read the post MVC With Lazy Loading and i still have some questions:
If we use the 4th choice, which part should be in charge of the transform process? The controller or the service? If we use controller, is it good to introduce #Transactional to controller?
I also see a post that suggested to use different query for different usage. It seems like to use different fetch groups JDO fetchgroup for different purpose. Is it good to use this way?
Thanks.
In today's day and age you can and should expose services as REST controllers and return proper domain objects rather than ModelAndView or other such constructs from Spring MVC.
UPDATE: Clarification: There is NO controller class in this approach I am suggesting. Expose Service as #Controller. Expose public methods on service as REST and annotate transnational, authorization etc context on the method. Because from an API point of view this public interface serves all kinds of clients whether it is REST or direct method call.
Also if you have dedicated controllers and services you may see some business logic seeping into your controllers in no time.
I would go to even further and not use option 4 which is essentially leads to a DTO anti-pattern.
Having said that it still depends on the complexity of entities. A very complex entity with many association is going to be a performance hog due to the queries it fires. Yet your MVC (JSPs) may actually resolve the associations whenever needed. (In a side note with full REST full architecture this is an issue.)

Spring mvc - get data from other server, what object to use and how to reuse it?

I have some url that I need to read data from there and use it in my controller.
Usually in java application I use http client, to get data from some url.
My questions are:
What object to use in spring mvc to get data from some url (like http client) ?
How to reuse this objects, so every time not to create it ?
Thank you!
In agreement with the comment by #Evgeny and #Beau above, you can use any client library you like. HttpClient is VERY bean friendly and, for cases where it might be difficult to construct the configuration, you can always provide a Spring factory bean to construct the object.
If you are looking to abstract away the plumbing of the HttpClient API usage, utilize the RestTemplate suggested by #Evgeny (I believe that it is also his brainchild) It is a VERY rich and simple API to leverage.

Resources