简体   繁体   English

无法向 springboot 发送 http post 请求

[英]Cannot send http post request to springboot

When I send an HTTP post request to spring boot rest API from my angular application, request is failing with below error当我从我的 Angular 应用程序向 spring boot rest API 发送 HTTP post 请求时,请求失败并出现以下错误

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 Spring启动控制台错误

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如此解决方案所提到的,我已将必要的标头添加到来自 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作为这个答案,我在模型对象中添加了getters/setters

I want to know where i went wrong and how to resolve this issue?我想知道我哪里出错了以及如何解决这个问题?

UPDATE更新

Springboot Rest Controller method Springboot Rest Controller 方法

@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());
    }
  }

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 ?您是否有不想使用 RequestMapping 的原因?

Is there any reasons not to add produces and consumes properties?是否有任何理由不添加producesconsumes属性?

@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?您是否添加了任何 JSON 消息转换器,例如 jackson? Check your application has below dependency检查您的应用程序具有以下依赖项

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

your return object should be converted to JSON properly.您的返回对象应正确转换为 JSON。 Add Jackson to you pom (separately from starter web) and retry.将 Jackson 添加到您的 pom(与入门网站分开)并重试。

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.2</version>
</dependency>

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.根据您最近的编辑,您现在收到406 Not Acceptable ,要修复此错误,请将 Spring boot 应用程序响应的媒体类型与客户端的Accept标头保持相同。 Try the following.请尝试以下操作。

  1. Change the value of produces to MediaType.APPLICATION_JSON as you have accept header in client as "application/json".将产品的值更改为MediaType.APPLICATION_JSON,因为您已将客户端中的标头作为“application/json”接受。 Also please note:另请注意:

APPLICATION_JSON_UTF8 is deprecated in favor of APPLICATION_JSON APPLICATION_JSON_UTF8 已被弃用,取而代之的是 APPLICATION_JSON

Reference: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/MediaType.html参考: 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.对于那些面临类似问题的人,您还可以检查 Accept header 中是否有任何错字,因为我经常遇到这个问题。

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在发送请求时使内容类型为 json 而不是文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM