首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring上默认为‘application/octet’,设置为null

在Spring上默认为‘application/octet’,设置为null
EN

Stack Overflow用户
提问于 2020-05-08 16:25:28
回答 1查看 2.2K关注 0票数 1

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

代码语言:javascript
复制
@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的响应.

代码语言:javascript
复制
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

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

如果我明确定义“消耗”属性..。

代码语言:javascript
复制
@RequestMapping(path="/process-contact-form", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)

春靴还这个..。

代码语言:javascript
复制
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported

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

我是在aws-无服务器-java容器https://github.com/awslabs/aws-serverless-java-container/wiki/Quick-start---Spring-Boot上构建的,我是从SAM本地CLI运行它的。

编辑

我切换日志到调试以获得更多的细节..。

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-10-16 01:04:20

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

代码语言:javascript
复制
@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
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61683553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档