0

When I send an HTTP post request to spring boot rest API from my angular application, request is failing with below error

Browser Error

    HTTP Status 415 – Unsupported Media Type
    Type Status Report

    Description: The origin server is refusing to service the request because the payload is in a format 
    not supported by this method on the target resource

Spring boot console error

java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.LinkedHashMap
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187) ~[spring-webmvc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203) ~[spring-webmvc-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.3.8.RELEASE.jar:4.3.8.RELEASE]
        .......

What I have tried so far

As this solution mentioned, i have added the necessary headers to the request from angular end

this.http.post(ipUrl, args, { headers: new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json', 'Access-Control-Allow-Headers': 'Content-Type'})});

As this answer, I have added getters/setters to the Model objects

I want to know where i went wrong and how to resolve this issue?

UPDATE

Springboot Rest Controller method

@PostMapping("/login")
  public @ResponseBody ResponseWrapper<WebUser> login(@RequestBody LoginData loginData){
    try {
      return loginService.loginProcess(loginData);
    }
    catch (Exception ex){
      ProgrammerAlert.printStackTrace(ex);
      return new ResponseWrapper<>(ResponseWrapper.ERROR, ex.getMessage());
    }
  }
Nibras
  • 329
  • 1
  • 6
  • 23

5 Answers5

0

Could you write your controller in this way. and see if it responds.

 @ResponseBody
 @RequestMapping(value = "/login",
        method = RequestMethod.POST)
 public  ResponseWrapper<WebUser> login(...){
     .
     .
     .
  }

Is there a reason that you do not want to use RequestMapping ?

0

Is there any reasons not to add produces and consumes properties?

@PostMapping("/login", produces = MediaType.APPLICATION_JSON_UTF8, consumes = MediaType.APPLICATION_JSON_UTF8)
public @ResponseBody ResponseWrapper<WebUser> login(@RequestBody LoginData loginData){
  try {
    return loginService.loginProcess(loginData);
  }
  catch (Exception ex){
    ProgrammerAlert.printStackTrace(ex);
    return new ResponseWrapper<>(ResponseWrapper.ERROR, ex.getMessage());
  }
}

or,

Did you add any JSON message converter, like jackson? Check your application has below dependency

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • I have changed my controller as you mentioned. Now spring boot console not showing any errors. But browser giving `HTTP 406` error code. any idea of why it's giving the error? – Nibras Jan 23 '20 at 06:25
  • @Junyoung answer is correct, please refer my updated answer to correct the Accept header alone, produces should be "application/json" - same as accept header – prakashb Jan 23 '20 at 07:33
0

your return object should be converted to JSON properly. Add Jackson to you pom (separately from starter web) and retry.

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.2</version>
</dependency>
i_sapumal
  • 222
  • 2
  • 10
  • First, I got exception in spring boot console and `http-415` error in browser. After add `produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE` to controller annotation both issues were fixed. But now i'm getting `http-406` error – Nibras Jan 23 '20 at 06:36
  • It is because your response from the server is still Not Acceptable from browser. Make sure you are receiving a Json response by Network tab in developer tools or by using postman like tool. – i_sapumal Jan 23 '20 at 06:43
  • In the browser network tab or postman, I'm getting empty response. – Nibras Jan 23 '20 at 06:50
  • I don't think my request is getting processed by server. I have added some `System.out.println()`. It's not printing anything – Nibras Jan 23 '20 at 06:52
  • debug line by line. use debug tools in your IDE. – i_sapumal Jan 23 '20 at 06:55
0

As per your recent edit, you're now getting 406 Not Acceptable, to fix this error, keep the media type of your Spring boot application's response same as the Accept header of your client. Try the following.

  1. Change the value of produces to MediaType.APPLICATION_JSON as you have accept header in client as "application/json". Also please note:

APPLICATION_JSON_UTF8 is deprecated in favor of APPLICATION_JSON

Reference: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/MediaType.html

  1. For those who are facing a similar issue, you can also check if there is any typo in Accept header as I often face this problem.
prakashb
  • 160
  • 1
  • 8
0

I also came across the same issue, I guess the answer of this question is very clear

HTTP Status 415 – Unsupported Media Type

while sending the request make the content type json instead of text

Ashwani kumar
  • 99
  • 1
  • 11