Which method does Spring Data Rest use to extract entity from query parameter? - spring

For example, we have a request like this:
GET /orders/search
{
"user": "http://example.org/users/1",
...
}
In what way does Spring Data Rest retrieve the User Entity from an URL? Does it use Regex to retrieve the id 1 then query it or something else?

If yo are wondering about spring data repository, you need to use this method.
User user = userRepository.findById(Integer id);

Related

Spring boot Reactive Mongo Query Interceptor

Is there anyway to intercept the query before executing it ?
for example
I have a userId header in the request and a document (let say book), with the userId stored within its attributes and persisted in the database
is there anyway to inject the userId criteria in all queries in a specific repository ( in this case BookRepository) so that I have a default query where
userId : {userId}
, or I just have to add the criteria in every function ?

How can i hide some attribute conditionally from json response

how can write condition 'if' type is no details other two fields are hide in json response. if possible i want to do it in pojo or bean ?
Using spring boot
spring data rest and hal.
pojo
MongoDB Repository
I want to show accountNo and Accountdetails if type="details"
{
"Name":"Json",
"lastName":"Amazon",
"type":"Details",
"accountNo":"12123",
"AccountdetailsDetails":[ some details]
}
If type="noDetails" just show mentioned response.
{
"Name":"Json",
"lastName":"Amazon",
"type":"NoDetails"
}
I guess you need #JsonFilter.
You can use this annotation to exclude some properties in your entity response.
What you need to do is Add this annotation with unique name in your entity file.
Then serialize this entity values using filter class SimpleFilterProvider.
Take a look on
https://www.logicbig.com/tutorials/misc/jackson/json-filter-annotation.html

Putting multiple POST API in a single Resource class in Spring boot application

I am going to implement a POST API in an existing resource class StudentResource, which contains already a POST REST API in order to create a Student.
The new POST API is basically for validating a Student entity, of the following form:
POST /api/student/validate
Request Body:
{
student_id : <>,
validation_type : <>
}
Now, I am not able to understand, should I create a new resource class: StudentValidationResource to put this new validate REST API or should I put this in the existing StudentResource.
EDIT:
The intent of the question is actually whether should I put more than one POST API in a single resource class.
Could anyone please help here?
Have you think about using validator? With javax #valid directly for your controller params / body?
You juste have to set the constraints in the object if the post receive an object, or directly in the controller like
#PostMapping("someurl")
public void myController(#valid #requestParams #size(min=2, Max=10) String aString) {}
I have a doubt if valid come before or after the requestParams.

right way to retrieve query parameters in Spring Boot rest?

I am developing REST api using Spring Boot. I've a controller which accepts POST requests.
http://localhost:8085/carride/end-ride
In the above request i want to access the parameter ride_transection_id for finding particular transection object and also some other value as well.
So basically i have 3 way to do that.
1. i can use #PathVariable
#RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(#PathVariable("ride_transection_id") long ride_transection_id,#RequestBody
SomeDTORequest someDTORequest ) {
//find transaction using path varibale
}
2.i can use #RequestParam
#RequestMapping(value = "/end-ride", method = RequestMethod.POST
public #ResponseBody item getitem(#RequestParam("ride_transection_id")
long ride_transection_id,#RequestBody SomeDTORequest someDTORequest ){
//find transaction using RequestParam varibale
}
i can use DTO Object SomeDTORequest and accept ride_transection_id into that with other value as well.
#RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(#RequestBody SomeDTORequest someDTORequest ) {
//find transaction using path someDTORequest .getID()
}
i am little bit confuses.just want ask which is safest and right way to access the ride_transection_id ?
thanks
You can use any of them but every way is designed for a certain use.
Path variable:
is used when you need to access an entity using a certain field for example i want to access an order and this order is defined by id so to access this order i need the following request Get /order/{id}
Request Parameter:
when you want to send a specific variable or flag for a certain method
for example Get /orders?is_shipped=true, so this will get all shipped orders or you may need orders at certain page Get /orders?page=1
Request body:
when you need to update the entity by the put or patch request as you will update the entity using the entity's json representation which can be send through the request body
for example PUT /orders/{id}
body: {"title": "order_1"}
then the order with id {id} will be updated with the new title
Spring data rest
See also
Basically, all these 3 methods are fine. But if you want to develop or design RESTful services with best practices, I strongly recommend you should provide the querying service with #PathVariable and GET method such as GET /tickets/12. Otherwise, to digest request body with #RequestBody annotation to retrieve querying criteria for POST method is the second suggestion.
Because POST method is usually to be used for creating something. And for querying something, both #PathVariable and #RequestParam annotations are suitable for GET method. More specifically, #RequestParam is often to be used in Filtering, Sorting and Searching results. For example:
Filtering: GET /tickets?state=open - Here, state is a query parameter that implements a filter.
Sorting: GET /tickets?sort=-priority,created_at - Retrieves a list of tickets in descending order of priority. Within a specific priority, older tickets are ordered first.
Searching: GET /tickets?state=closed&sort=-updated_at - Retrieve recently closed tickets.
Please also refer to this article Best Practices for Designing a Pragmatic RESTful API.
Hope this helps you! :)

Spring Data Rest Mongo - how to create a DBRef using an id instead of a URI?

I have the following entity, that references another entity.
class Foo {
String id;
String name supplierName;
**#DBRef** TemplateSchema templateSchema;
...
}
I want to be able to use the following JSON (or similar) to create a new entity.
{
"supplierName": "Stormkind",
"templateSchema": "572878138b749120341e6cbf"
}
...but it looks like Spring forces you to use a URI like this:
{
"supplierName": "Stormkind",
"templateSchema": "/template-schema/572878138b749120341e6cbf"
}
Is there a way to create the DBRef by posting an ID instead of a URI?
Thanks!
In REST, the only form of ID's that exist are URIs (hence the name Unique Resource Identifier). Something like 572878138b749120341e6cbf does not identify a resource, /template-schema/572878138b749120341e6cbf does.
On the HTTP level, entities do not exist, only resources identified by URIs. That's why Spring Data REST expects you to use URIs as identifiers.

Resources