Michael H
Michael H

Reputation: 215

Swagger Codegen - Content Type for GET not getting set in Spring-MVC generated

Downloaded Swagger Pet Store (Simple) from swagger.io editor page and did a mvn jetty:run

When opening up DevTools on Chrome and exercising a GET, I don't see the header Content-Type field being set.

This is on the GET /pets{id}

screen capture

Upvotes: 5

Views: 11903

Answers (3)

Sarvar Nishonboyev
Sarvar Nishonboyev

Reputation: 13110

According to the HTTP/1.1 specification, neither GET or DELETE request should have a message body, and if they have one, the server should ignore it. I think that's why swagger-UI doesn't send a Content-Type header with GET or DELETE requests.

More here: https://www.rfc-editor.org/rfc/rfc2616#section-4.3

Upvotes: 0

Michael H
Michael H

Reputation: 215

OK, found the root cause of this problem thanks to wing328 on the swagger codegen project on github.

It turns out that at the top of our yaml definition file we have:

consumes:
 - application/json

That causes all generated controller methods to expect application/json on content-type. This breaks GET, DELETE, and PATCH api calls which in our case do not have any payload.

Upvotes: 16

Roger Willcocks
Roger Willcocks

Reputation: 1667

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

14.17 Content-Type

The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the case of the HEAD method, the media type that would have been sent had the request been a GET. Content-Type = "Content-Type" ":" media-type

Content-Type is a meaningless header in the context of a request. Doubly so for a "GET" since the request has no body.

If you could post Request and Response headers and body for both Chrome and curl, I might be able to resolve this for you.

Having said that, looking around StackOverflow quickly tells me that Spring can raise this error for multiple reasons unrelated to content encodings. Such as (for instance) the object you are trying to serialize lacking a parameter less constructor.

Do you have any logging set up that would trace the InnerExceptions and stack to see what the base error is?

Here's what Spring is doing with my app (not the petstore). Do you want screen shots of the from the swagger ui page for my app too?

Thanks for looking at this Roger.

2015-11-04 18:32:13.977 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hacker/api/mef/sca/v1/SCA_ETH_FDFr_EC/5]
2015-11-04 18:32:13.977 DEBUG 32416 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /api/mef/sca/v1/SCA_ETH_FDFr_EC/5
2015-11-04 18:32:13.980 DEBUG 32416 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'null' not supported
2015-11-04 18:32:13.980 DEBUG 32416 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: public void com.ciena.sca.exception.ApiExceptionHandler.handleOtherErrors(java.lang.Exception,javax.servlet.http.HttpServletResponse)
2015-11-04 18:32:13.980 ERROR 32416 --- [nio-8080-exec-1] c.c.sca.exception.ApiExceptionHandler    : Content type 'null' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'null' not supported
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:231)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:349)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:296)
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:299)
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1120)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:932)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter.doFilterInternal(EndpointWebMvcAutoConfiguration.java:299)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:102)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:76)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
2015-11-04 18:32:13.982 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2015-11-04 18:32:13.982 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2015-11-04 18:32:13.983 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/hacker/error]
2015-11-04 18:32:13.983 DEBUG 32416 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2015-11-04 18:32:13.983 DEBUG 32416 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2015-11-04 18:32:13.983 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Last-Modified value for [/hacker/error] is: -1
2015-11-04 18:32:13.985 DEBUG 32416 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Written [{timestamp=Wed Nov 04 18:32:13 EST 2015, status=400, error=Bad Request, exception=org.springframework.web.HttpMediaTypeNotSupportedException, message=Content type 'null' not supported, path=/hacker/api/mef/sca/v1/SCA_ETH_FDFr_EC/5}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2ed7a433]
2015-11-04 18:32:13.985 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2015-11-04 18:32:13.985 DEBUG 32416 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Successfully completed request

Upvotes: 0

Related Questions