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

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.

Related

Spring RestController endpoint that consumes and produces a String value returning "HTTP Status 415 – Unsupported Media Type"

I'm trying to setup a fairly simple endpoint that takes in String and produces a String. I've simplified it down to:
#RequestMapping(value="/foo",
method = RequestMethod.GET,
consumes = MediaType.TEXT_PLAIN_VALUE,
produces= MediaType.TEXT_PLAIN_VALUE)
public String foo(#RequestBody String data) {
return data;
}
I'm testing with Postman and am just getting "HTTP Status 415 – Unsupported Media Type" back. This app is running on Spring 4.1.3. I copied this endpoint into another project I have in Spring Boot, using my same Postman request (just modified the host), and it works there. So there appears to be some issue with the version of Spring this project uses. As a work-around I created a simple wrapper entity (POJO that only consists of a single String) and changed the endpoint to consume JSON/this POJO. Does anyone know specifically why it doesn't work as above and/or know of a workaround that'll let me send and receive a plain text String with this version of Spring?

How to log 415 errors in Spring

My service is currently experiencing 415 errors, but I'm not getting any logs to find out what errors they are.
Will I be able to add logging in some kind of filter so that I can know what's going on?
#RequiresHttps
#RequestMapping(value = "/test", method = RequestMethod.POST)
public ResponseEntity<String> doAction(#NonNull #RequestBody CustomRequest[] requests) {
It looks like the 415 is happening inside spring mvc engine and it doesn't event reach your controller so that you can't really place the logs in our code (in doAction method for example).
Try to enable tomcat embedded access logs and you'll see the file with all the requests and return status. These are disabled by default so you should add the following into application.properties or yaml:
server.tomcat.accesslog.enabled=true
There are some configurations / customizations you can do with that, you can read about them in this tutorial for example

Spring MVC GET Request Logging

I have a Spring Boot/Spring MVC REST app that has a GET mapping endpoint for ex.
#GetMapping(value = "/person")
public ResponseEntity<Person> getPerson(#RequestParam final String personID)
{
//service call for a person
return new ResponseEntity.ok(personObj);
}
I am using PostMan to hit the endpoint and I have the spring boot jar running and something I noticed was that every PUT and POST method is logged but when it came to GET requests, they were not logged. The only time I got a GET in the running spring boot server logs was if I hit the wrong endpoint path for i.e localhost:8080/personn (misspelling) the log would show the URL with the requestparam value appended to the URL like "GET /personn?personID=123 HTTP/1.1" 404 - "-" "PostmanRuntime
It seems like successful GET calls are not logged but unsuccessful GET calls are logged. Is this normal behavior?
If personID was sensitive data then I should probably use POST?
You can set the logging level of the Spring web package to DEBUG in the application.properties, something like:
logging.level.org.springframework.web=debug
or in recent version of Spring Boot:
logging.level.web=debug

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/

Spring boot with SpringFox Swagger UI Generates all Verbs

I enabled API documentation using Swagger 2.6.1 in my spring boot app. The page(swagger-ui.html) loads fine but controller documentation contains all the verbs(PUT, GET, PATCH, POST, etc) even if my controller only has a GET operation. How can I disable the other verbs in the UI doc?
This happens when you have mapping like this in your controller
#RequestMapping(value = "/productDetails")
Springfox cannot identify what is the requestMethod hence it provides all mapping.(Eventhough the default is GET)
If you change this to
#RequestMapping(value = "/productDetails", method = RequestMethod.GET)
Then you will see only GET mapping and not others.
If you use newer versions of Sprinboot, you can use #GetMapping or #PostMapping instead of #RequestMapping

Resources