lab*_*jee 5 spring-mvc spring-boot
使用Spring Boot(v1.2.6.RELEASE),我需要接受对Controller方法的POST请求.
@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){
Run Code Online (Sandbox Code Playgroud)
...}
当我从Chrome/mozilla的REST客户端发送POST请求时,响应如下:
{"timestamp":1446008095543,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/myWebApp/signup/registerFamily"}
...
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪如下:
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)
Run Code Online (Sandbox Code Playgroud)
在Spring的响应之后:不接受mvc:resources下的POST请求?如何解决这个问题,
我认为WebContentGenerator的其他一些子类,如WebContentInterceptor,RequestMappingHandlerAdapter 应该处理对控制器的请求.
此外,在运行Junit测试时,我发现RequestMappingHandlerAdapter正在处理对控制器的这些请求.
但是我还没有找到如何配置它来接受POST请求呢?
任何其他更好的解决方案都应该受到欢迎.
编辑:
Tomcat使用WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter(WebMvcConfigurerAdapter的子类)和
WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.addResourceHandlers(ResourceHandlerRegistry)执行以下操作:
oswshandler.SimpleUrlHandlerMapping:映射的URL路径[/ webjars/**]到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
oswshandler.SimpleUrlHandlerMapping:将URL路径[/**]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
在junit中,我添加了一个空的具体子类WebMvcConfigurerAdapter.
和WebMvcConfigurerAdapter.addResourceHandlers(ResourceHandlerRegistry)什么都不做.
@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 { ... }
Run Code Online (Sandbox Code Playgroud)
很难说出错了,因为你的代码看起来很好.但我会尝试提出一些小改动,这可能有助于澄清情况.
首先,@ResponseBody您在使用时不需要额外的注释@RestController.从春季网站:
Spring 4的新@RestController注释,它将类标记为控制器,其中每个方法都返回一个域对象而不是视图.这是@Controller和@ResponseBody汇总在一起的简写.
此外,问题可能隐藏在您的headers要求之下.如果不具体,可以将其更改为consumes选项.试试下面的代码段.
@RequestMapping(value = "/api", method = RequestMethod.POST,
consumes = "application/json", produces = "application/json")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32786 次 |
| 最近记录: |