当前位置: 首页 > 知识库问答 >
问题:

“Consumes”在Spring Boot RequestMapping上默认为“application/octet stream”,设置为空

阙项禹
2023-03-14

我试图在Spring Boot中创建一个使用JSON的简单Post请求处理程序。

@RestController
@EnableWebMvc
public class WebFormController
{
    @RequestMapping(path="/process-contact-form", method = RequestMethod.POST)
    public ResponseEntity<String> processContact(@RequestBody Form form) throws Exception
    {
        System.out.println("TEST TEST: Ran");
        return  new ResponseEntity<String>("Thank you for contacting us, we'll respond soon.", HttpStatus.OK);
    }
}

默认情况下,我的理解是这将使用application/json。然而,当我使用curl或Postman发出请求时,我从Spring得到了这个响应。。。

DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

在邮递员中,我得到了“415不支持的媒体类型”。

如果我明确定义“consumes”属性。。。

@RequestMapping(path="/process-contact-form", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)

Spring开机返回这个。。。

DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported

我尝试将consumes设置为“application/json”、x-www-form-urlencoded和纯文本,并从Postman发送这些类型的请求,但得到了相同的结果。

我用aws无服务器java容器构建了这个https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot 我从SAM本地CLI运行它。

编辑

我将日志记录切换到调试以获取更多详细信息。。。

2020-05-08 17:53:40.692 DEBUG 1 --- [           main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-05-08 17:53:40.692 DEBUG 1 --- [           main] c.a.s.p.i.servlet.FilterChainHolder      : Starting REQUEST: filter 0-characterEncodingFilter
2020-05-08 17:53:40.698 DEBUG 1 --- [           main] c.a.s.p.i.s.AwsProxyHttpServletRequest   : Called set character encoding to UTF-8 on a request without a content type. Character encoding will not be set
2020-05-08 17:53:40.698 DEBUG 1 --- [           main] c.a.s.p.i.servlet.FilterChainHolder      : Starting REQUEST: filter 2-com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter
2020-05-08 17:53:40.729 DEBUG 1 --- [           main] o.s.web.servlet.DispatcherServlet        : POST "/process-contact-form", parameters={}
2020-05-08 17:53:40.732 DEBUG 1 --- [           main] c.a.s.p.i.servlet.AwsHttpServletRequest  : Trying to access session. Lambda functions are stateless and should not rely on the session
2020-05-08 17:53:40.758 DEBUG 1 --- [           main] c.a.s.p.i.s.AwsHttpServletResponse       : Response buffer flushed with 0 bytes, latch=1
2020-05-08 17:53:40.761  WARN 1 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]
2020-05-08 17:53:40.761 DEBUG 1 --- [           main] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 415
2020-05-08 17:53:40.763 DEBUG 1 --- [           main] c.a.s.p.i.servlet.AwsHttpServletRequest  : Trying to access session. Lambda functions are stateless and should not rely on the session
2020-05-08 17:53:40.768 DEBUG 1 --- [           main] c.a.s.p.i.servlet.FilterChainHolder      : Executed ERROR: filter 3-com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter
2020-05-08 17:53:40.769 DEBUG 1 --- [           main] c.a.s.p.i.servlet.FilterChainHolder      : Executed ERROR: filter 3-characterEncodingFilter
2020-05-08 17:53:40.779  INFO 1 --- [           main] c.a.s.p.internal.LambdaContainerHandler  : 127.0.0.1:56893 null- null [08/05/2020:17:53:40Z] "POST /process-contact-form null" 415 - "-" "-" combined
2020-05-08 17:53:40.818 DEBUG 1 --- [           main] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@c9d82f99 pairs: {POST /2018-06-01/runtime/invocation/807c7e06-86f6-1bd3-1055-78b464604573/response HTTP/1.1: null}{Docker-Lambda-Invoke-Wait: 1588960412475}{Docker-Lambda-Init-End: 1588960420587}{User-Agent: Java/1.8.0_201}{Host: 127.0.0.1:9001}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}{Content-type: application/x-www-form-urlencoded}{Content-Length: 104}
2020-05-08 17:53:40.822 DEBUG 1 --- [           main] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@6f0129144 pairs: {null: HTTP/1.1 202 Accepted}{Content-Type: application/json}{Date: Fri, 08 May 2020 17:53:40 GMT}{Transfer-Encoding: chunked}
2020-05-08 17:53:40.825 DEBUG 1 --- [           main] s.n.www.protocol.http.HttpURLConnection  : sun.net.www.MessageHeader@18fdb6cf5 pairs: {GET /2018-06-01/runtime/invocation/next HTTP/1.1: null}{User-Agent: Java/1.8.0_201}{Host: 127.0.0.1:9001}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}

编辑

StreamLambdaHandler

public class StreamLambdaHandler implements RequestStreamHandler {
    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(Application.class);
        } catch (ContainerInitializationException e) {
            // if we fail here. We re-throw the exception to force another cold start
            e.printStackTrace();
            throw new RuntimeException("Could not initialize Spring Boot application", e);
        }
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
            throws IOException {
        System.out.println("TEST TEST " + inputStream.toString() + " " + outputStream.toString());
        handler.proxyStream(inputStream, outputStream, context);
    }
}

共有1个答案

袁单鹗
2023-03-14

我有一些帮助从头开始应用程序,并使用不同的库。注释现在看起来像这样...

@Path("/submit")
@Component
@Slf4j
public class SubmitController {

    private JiraService jira;
    private ConfigurationRepository configurations;

    @Autowired
    public SubmitController(JiraService jira, ConfigurationRepository configurations) {
        this.jira = jira;
        this.configurations = configurations;
    }

    @POST
    @Path("/{id}")
    @AnonymousAllowed
    @Produces({APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response submitForm(@PathParam("id") String id, String body) {
        // Stuff
    }
 类似资料:
  • Apache Kafka文档说明: 内部Kafka Streams使用者max.poll.interval.ms默认值已从300000更改为integer.max_value

  • 问题内容: 我要在这里做的是 使python3成为默认的python。 除了在Mac上自动安装的python 2.7之外,我还使用 homebrew 安装了 python3 。这是我关注的网站。http://docs.python-guide.org/zh- CN/latest/starting/install3/osx/#install3-osx 我想我很好地遵循了每条指令,重新安装了xcode

  • 在安装了以管理中的冲突后,仍然会为我打开默认设置: 以下是本回答中提到的配置: 注意:我可以用简单的文件单独运行,但是我不能在我的git项目中运行它。任何想法? 编辑:这里是我的. gitconfig:

  • 问题内容: 有没有办法将CentOS 7上的Python 3.5.2设置为默认的Python版本?目前,我已经默认安装了Python 2.7和Python 3.5.2。 我使用了以下命令 但是之后给出了错误。 问题答案: 如果这 不起作用(应该) 您可以使用以下命令在您的计算机中添加一个别名: 如果这不起作用,则应该只使用虚拟环境。阅读此页以开始使用。

  • 问题内容: 可以为JPA中的列设置默认值吗?如果使用注释,如何做到这一点? 问题答案: 实际上,在JPA中是可能的,尽管使用批注的属性有些小小的改动,例如:

  • 问题内容: 尝试使用WTForms设置SelectField的默认值时,我像这样将值传递给“默认”参数。 我也尝试了以下方法。 都不将默认选定字段设置为“ Def”。这适用于其他类型的字段,例如TextField。如何设置SelectField的默认值? 问题答案: 你发布的第一种方法是正确的,并且对我有用。唯一无法解释的原因可能是你正在运行旧版本的WTForms,它在1.0.1上对我有效