Handle Unsupported Media Types in Spring - spring

I'm currently building a simple REST service using Spring framework. But when testing using Postman, I have encountered the following issue:
Click to view image
I wonder how to make Spring recognize the received Content-Type header actually match the application/x-www-form-urlencoded, which is supported.
UPDATED:
I'm current run my app at localhost with embedded Tomcat server.
My endpoint briefly includes:
POST: /api/users - for creating new user
GET: /api/users - for listing users
GET: /api/users/{id} - for retrieving user by his id
The request body for POST endpoint:
"email":"<your-email>",
"password:"<your-password>"
UPDATED 2:
My Endpoint image

Try this:
#PostMapping(path = "/api/users",consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<String> createUser(#RequestBody User user)

Related

How to access Request headers in Spring Web services PayloadValidatingInterceptor

I am currently working on a Spring Web services(SOAP) project. The requirement is to validate the request payload and return the error response if validation fails and log it. I have extended PayloadValidatingInterceptor to return custom validation message.
As part of logging requirement, we need to print the header values from Request Headers (HttpServletRequest) for tracking purpose.
How to access HttpServletRequest in the implementation of PayloadValidatingInterceptor?
Is it possible to inject HttpServletRequest?
version details:
Spring-Boot : 2.2.6
Spring-ws-core : 3.0.8
Please help.

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

Spring cloud Feign:no suitable HttpMessageConverter found for response type [class org.springframework.web.servlet.ModelAndView]

I have a service which has a url that return ModelAndView Object.
In its own point, I can access the website. But when I use spring cloud feign to invoke that url, it comes out that no suitable HttpMessageConverter found for response type [class org.springframework.web.servlet.ModelAndView] and contentType text/html. Here is my feign client.
Please try to change empList() method in your ConsumerController class like below.
public String empList() {
return empService.empList();
}
ModelAndView is not the actual response of /emplist from EmpController. It will be handled by DispatchServlet and ViewResolver will resolve the actual view with your view name - emp. So, from the view of ConsumerController, The response will be String object.
Anyway, I'm not sure that it's a good idea that accessing another web page via feign client in your case. Because if the original html page contains additional resources like images that existing in your origin server, it will not be served.

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.

Resources