Using Spring Boot (v1.2.6.RELEASE), I need to accept POST requests for the Controller methods.
@RestController
@RequestMapping(value = "/myWebApp/signup")
public class RegController {
@RequestMapping(value = "/registerFamily", method = { RequestMethod.POST }, headers = {"Content-type=application/json"})
@ResponseBody
public FamilylResponse registerFamily(@RequestBody @Valid FamilyRequest familyRequest){
... }
When I send POST request from REST clients from Chrome/mozilla,the response comes as follows:
{"timestamp":1446008095543,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/myWebApp/signup/registerFamily"}
...
The stack trace is as follows:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
at org.springframework.web.servlet.support.WebContentGenerator.checkAndPrepare(WebContentGenerator.java:273)
at org.springframework.web.servlet.support.WebContentGenerator.checkAndPrepare(WebContentGenerator.java:251)
at org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:207)
at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:51)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
Following the response of Spring: not accept POST request under mvc:resources? how to fix that,
I think some other sub class of WebContentGenerator, like WebContentInterceptor, RequestMappingHandlerAdapter should handle the requests for Controllers.
Also while running Junit tests, I found that the RequestMappingHandlerAdapter is handling those requests for Controllers.
But I'm yet to find how to configure this to accept POST requests as well?
Any other better solution should be more than welcome.
EDIT:
Tomcat uses WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter(a subclass of WebMvcConfigurerAdapter) and
WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers(ResourceHandlerRegistry) does the following:
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
In junit, I have added an empty concrete child class of WebMvcConfigurerAdapter.
and WebMvcConfigurerAdapter.addResourceHandlers(ResourceHandlerRegistry) does nothing.
@Configuration
@EnableWebMvc
public class WebAppContext extends WebMvcConfigurerAdapter {}
@Configuration
@Import({ WebAppContext.class })
@PropertySource("classpath:application.properties")
public class ExampleApplicationContext { }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ExampleApplicationContext.class})
@WebAppConfiguration
public class RegControllerTest { ... }