When did you use #RequestBody and #Pathvariable and #RequestParam - spring-boot

I have some application using Micro service architecture in Spring boot. I have send the query parameter, Object, Model and etc using #PathVariable in RestTemplate. After developed the application, I did some research and there are asked to use #RequestParam and #RequestBody. But I can't uanble to understand and also I don't know how to use #RequestBody and #RequestParam. What are benefits while using #RequestBody instead of #PathVariable.
I have send the GET request using Spring boot RestTemplate.

In short, Path variable and request params will be part of the URL and the request body will be part of body of the request.
When you want to locate a resource then go for #PathVariable i.e. fetch/delete a user by id.
/users/{id}
When you want to query/search/filter the data then go for query parameters.
users?lastName=abc
When you want to save a user then go for request body.
{
"username" : "abc",
"email" : "abc#gmail.com"
}

Related

How to post multipart content type in single request body using spring and restful service

We have a requirement to post a multiformats request type to send to a resful api..I am a beginer and need help with a sample program hiw to post multiple requests with different format like json and pdf in single http request so that I can get a response back?can anyone please post a sample code how to implement multi part content type?
I've had already developed an upload microservice using multipart/form-data requests. The example bellow is a simple endpoint that accepts multiple files using multipart/form-data. You only need to use the spring MultipartFile class as the argument.
#PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity upload(
#RequestPart(value = "files") MultipartFile[] files) {
return ResponseEntity.ok().build();
}
For more details I highly recommend reading the spring official guide in uploading files: https://spring.io/guides/gs/uploading-files/

How to send POST requests to a Spring Boot REST API using Postman form-data?

I'm trying a very simple Spring Boot + MySql app using STS: https://github.com/acenelio/person01
Posting a new Person using raw body works fine. However, when trying to post using form-data, I get either a 400 HttpMessageNotReadableException or 415 HttpMediaTypeNotSupportedException error, depending on which Content-type I set on Headers.
I provided some Postman printscreens here: https://sites.google.com/site/stackoverflownelioalves/arqs
What am I missing? I'm kinda new on Postman and Spring Boot. Thanks.
You can use the #RequestBody annotation:
#RequestMapping("/addInvestorProfile")
public #ResponseBody Boolean addInvestorProfile(#RequestBody() InvestorProfile profile
, #RequestParam(value = "sessionId") String sessionId)
{
.... your content
return true;
}
I am not sure which version of your spring boot. First of all, almost all spring mvc or spring boot controller method will accept all request content-type when you are not add consumer param to #RequestMapping annotation. So, maybe there is some wrong usage of postman.And I am work with spring boot & postman well.
try to compare postman behavior by copy request to curl
Usually post a request use curl looks like:
curl -XPOST UrlOfYourSpringBootEndPoint -d'body of your post'
more detail of curl here.

The difference between exchange method and execute method in spring rest template?

I have three questions!
First.
I am using the spring framework for sending the data through rest protocol.
restTemplate.exchange(requestUrl,HttpMethod.POST, request, listVo.getClass());
org.springframework.web.client.RestTemplate.exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<? extends Object> responseType, Object... uriVariables) throws RestClientException
I used it without any problem, but I want to know the purpose of the parameter, responseType.
The client don't use response data, but just use response status code / msg. So, I sent some meaningless
String data instead. But the error thrown that they accept "null". So I sent a "null" String. not null.
Then, the error got rid of. But there was another problem. Right after the client received the data from the server and paused for a long time. Then next line of codes are executed. What is problem?
Second
I can't find any references that use execute method of Spring RestTemplate.
Third
Like the title, What is the difference between the exchange method and the execute method in spring rest template?
Thanks for your time and effort.
Cheers.
exchange return type is ResponseEntity<T> while execute is T
Taken from "Pivotal Certified Spring Web Application Developer Exam" book
The execute and exchange methods can be used for any type of REST calls
The execute method can also be given a RequestCallback implementation as a parameter, which tells the RestTemplate what to do with the request before sending it to the server.

Spring MVC interceptor

I need inputs related to Spring MVC, I have a URL to which a client will send a post request with an xml as the pay load. I plan to have a controller method which maps to the requested url, I want that xml to be validated/converted to an object using jaxb before controller method is executed. And also, the controller method should have only the object as the parameter to its methods and no httprequest etc.
So, how do I achieve this? Will interceptor be helpful? If yes, how will it be done?
I plan to use Spring 3.
Simply use #RequestBody in conjunction with #Valid on a method argument and that is all you need.
public void myRequestHandlingMethod(#Valid #RequestBody YourJaxbObject jaxbObject) { … }
I strongly suggest you take a look at the Spring reference guide

Spring get request data in Dao or service layer

I m sing spring data MongoDb. in controller #requestBody User user.
So There is no data in request now. After take it from the request body.
Is there any Way in spring to get the request body data after take it from the request Object in dao layer.. Please help me out. Thx in advance/..
Spring doesn't magically store it anywhere, so you have to read it yourself in the controller layer and pass it around as a regular parameter or store somewhere yourself.

Resources