Spring Boot - 请求方法 'POST' 不受支持。尝试了所有方法。

5
我将使用Spring Boot和MongoDB构建一个Web应用程序,用于执行员工文档的CRUD操作。
当我尝试使用JSON访问“创建员工”端点时,出现了“请求方法'POST'不受支持”的错误。
我的控制器类是:
@RestController
@RequestMapping("/employeeapp/employees")
public class EmployeeController {

    private final EmployeeService service;

    @Autowired
    public EmployeeController(EmployeeService service) {
        this.service = service;
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public
    @ResponseBody
    Employee create(@RequestBody @Valid Employee employeeEntry) {
        return service.create(employeeEntry);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Employee delete(@PathVariable("id") long id) {
        return service.delete(id);
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Employee> findAll() {
        return service.findAll();
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Employee findById(@PathVariable("id") long id) {
        return service.findById(id);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public Employee update(@RequestBody @Valid Employee employeeEntry) {
        return service.update(employeeEntry);
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public void handleEmployeeNotFound(EmployeeNotFoundException exception) {
    }

}

应用程序类:

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

我已经尝试禁用 csrf,在方法上添加@ResponseBody,但似乎没有任何作用。 编辑 我正在使用 POST 请求命中 http://localhost:8080/employeeapp/employees。头部包括 Content-Type : application/json 并携带以下 json 内容:
{
    "id" : 1,
    "name" : "nikhil",
    "dept" : "DCX"
}

此外,当我使用POST请求访问上述URL时,以下是日志中显示的内容。
2016-02-19 12:21:36.549  INFO 5148 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       :
Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-02-19 12:21:36.549  INFO 5148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        :
FrameworkServlet 'dispatcherServlet': initialization started
2016-02-19 12:21:36.562  INFO 5148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        :
FrameworkServlet 'dispatcherServlet': initialization completed in 13 ms
2016-02-19 12:21:36.595  WARN 5148 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound             :
Request method 'POST' not supported

编辑2:

我查看了Spring Boot日志,发现映射没有生成,而是Spring正在映射到默认服务。您有什么想法,为什么会发生这种情况?

    [INFO] --- spring-boot-maven-plugin:1.3.2.RELEASE:run (default-cli) @ EmployeeApp ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.2.RELEASE)

2016-02-19 14:51:00.690  INFO 5080 --- [           main] app.Application                          : Starting Application on DIN16003277 with PID 5080 (D:\!Nikhil\Documents\Code\EmployeeApp\target\classes started by nvibhav in D:\!Nikhil\Documents\Code\EmployeeApp)
2016-02-19 14:51:00.693  INFO 5080 --- [           main] app.Application                          : No active profile set, falling back to default profiles: default
2016-02-19 14:51:00.770  INFO 5080 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@46117566: startup date [Fri Feb 19 14:51:00 IST 2016]; root of context hierarchy
2016-02-19 14:51:01.987  INFO 5080 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-02-19 14:51:02.567  INFO 5080 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-02-19 14:51:03.026  INFO 5080 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-02-19 14:51:03.037  INFO 5080 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-02-19 14:51:03.039  INFO 5080 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-02-19 14:51:03.172  INFO 5080 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-02-19 14:51:03.173  INFO 5080 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2409 ms
2016-02-19 14:51:03.689  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'metricFilter' to: [/*]
2016-02-19 14:51:03.689  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-02-19 14:51:03.690  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-02-19 14:51:03.690  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-02-19 14:51:03.690  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-02-19 14:51:03.691  INFO 5080 --- [ost-startStop-1] .e.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2016-02-19 14:51:03.691  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2016-02-19 14:51:03.691  INFO 5080 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'applicationContextIdFilter' to: [/*]
2016-02-19 14:51:03.692  INFO 5080 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-02-19 14:51:04.011  INFO 5080 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration :

Using default security password: c652ec29-f926-40eb-bb5b-2bd9185bf6a5

2016-02-19 14:51:04.075  INFO 5080 --- [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []
2016-02-19 14:51:04.141  INFO 5080 --- [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@71d64e0f, org.springframework.security.web.context.SecurityContextPersistenceFilter@68e32d1f, org.springframework.security.web.header.HeaderWriterFilter@30bd43e4, org.springframework.security.web.authentication.logout.LogoutFilter@6a766ce6, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@3111b148, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@75e89f1f, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@289e0d8f, org.springframework.security.web.session.SessionManagementFilter@4ec4999b, org.springframework.security.web.access.ExceptionTranslationFilter@3f4e33f9]
2016-02-19 14:51:04.181  INFO 5080 --- [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration$LazyEndpointPathRequestMatcher@7f2a96e1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@3ae13235, org.springframework.security.web.context.SecurityContextPersistenceFilter@5f36bdc8, org.springframework.security.web.header.HeaderWriterFilter@658ee520, org.springframework.security.web.authentication.logout.LogoutFilter@1ce1dc64, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@51a29584, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@120723a8, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@b2632d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@49cabfed, org.springframework.security.web.session.SessionManagementFilter@6c8e082f, org.springframework.security.web.access.ExceptionTranslationFilter@51f381ff, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@3b3223fd]
2016-02-19 14:51:04.399  INFO 5080 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@46117566: startup date [Fri Feb 19 14:51:00 IST 2016]; root of context hierarchy
2016-02-19 14:51:04.471  INFO 5080 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto 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)
2016-02-19 14:51:04.472  INFO 5080 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-02-19 14:51:04.506  INFO 5080 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-19 14:51:04.506  INFO 5080 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-19 14:51:04.549  INFO 5080 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-19 14:51:04.720  INFO 5080 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2016-02-19 14:51:04.844  INFO 5080 --- [localhost:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:5}] to localhost:27017
2016-02-19 14:51:04.845  INFO 5080 --- [localhost:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 1]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=565904}
2016-02-19 14:51:05.243  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.244  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2016-02-19 14:51:05.244  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.245  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.247  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2016-02-19 14:51:05.247  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.250  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.251  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health || /health.json],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2016-02-19 14:51:05.251  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.252  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.252  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info || /info.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.253  INFO 5080 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-02-19 14:51:05.377  INFO 5080 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-19 14:51:05.391  INFO 5080 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-02-19 14:51:05.561  INFO 5080 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-02-19 14:51:05.567  INFO 5080 --- [           main] app.Application                          : Started Application in 5.207 seconds (JVM running for 11.036)

1
当您使用POST请求命中URL时,如果在控制器中没有相应的映射,则会出现此问题。您正在使用POST请求命中哪个URL?请在问题中提供该信息。 - Kunal Surana
你确定请求体和URL是正确的吗?你也可以尝试在你的类中添加@ExceptionHandler(HttpMessageNotReadableException.class) public ResponseEntity handleBadInput(HttpMessageNotReadableException ex) { Throwable cause = ex.getCause();来检查输入是否有问题。你在控制台中还有其他错误吗? - Gyan
你正在调用哪个URL,以及你的应用程序映射在哪个URL上。看起来你的映射URL是错误的,并且包括了DispatcherServlet的映射或应用程序名称。 - M. Deinum
我编辑了帖子并添加了我正在尝试访问的URL。应用程序映射到/employeeapp/employees。我基本上遵循了这里的文章。当我运行这个完全相同的项目时,它可以无缝运行。 - Nikhil Vibhav
2
你能在你的代码片段中添加包信息吗?也许你的控制器没有被Spring扫描到? - Brian Clozel
显示剩余3条评论
5个回答

12

以下方法可能有所帮助。

  1. 在部署应用程序时,Spring Boot 会在控制台上显示所有可用的服务。检查“POST”方法是否被显示出来。检查其他服务是否存在矛盾。GET/PUT/POST
  2. 如果没有部署服务,请尝试在其他服务之前添加“/”- GET、PUT、POST。
  3. 添加异常处理程序(链接)以检查客户端输入请求以检查POJO结构。
  4. 检查URL-是否添加了任何全局路径以及配置中的应用程序/名称。(链接:链接)
  5. 尝试删除请求标头中的Content-Type,并重新发布请求。

以下是可能发生的不同情况。无需按顺序遵循。

编辑:

运行以下命令,检查你的控制器是否被启用并被Spring应用程序考虑。

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

    }

原来,spring-boot没有将URL映射到服务。在上面的帖子中添加日志。 - Nikhil Vibhav
“log” 是在部署期间还是在请求期间? - Vinay Veluri
2
回复晚了,但我被工作占据了。是的,结果发现包结构是错误的。早些时候,我的代码在src/java下的子目录中。现在,将它们添加到com.nikhil.practice下就可以了。 - Nikhil Vibhav

5
如果你的@SpringBootApplication注解类和控制器在不同的包中,它默认不会进行类扫描。

在几乎所有的演示中,它都可以工作,因为引导类(@SpringBootApplication注解类)和rest控制器类位于同一包中,并加载到spring上下文中。
//像这样让你的类对spring可访问
@SpringBootApplication(scanBasePackageClasses = {GreetingController.class})
public class BootPocApplication {

或者 // 给出基本包名称

@SpringBootApplication(scanBasePackages = {"com.tryout"})
public class BootPocApplication {

是的,@ThusharaAriyasena。我几个月前就想到了。不管怎样,谢谢你的帮助 :) - Nikhil Vibhav

3
对我而言,问题出在 @RequestBody 类不可访问,因为它是一个内部类。
将其设置为 public/static 使其变得可访问并解决了这个问题。
public static class MyClassDto {
        public String   name;
        public String   comment;
        public String   key;
}

2

对于我来说,当我在方法定义中使用@RequestParam时,出现了这个错误:

@RequestMapping(value = "/balance",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public FinancialMsgResponse balance(@RequestParam(value = "requestMessage") FinancialMsgRequest requestMessage) throws Exception {

        return new FinancialMsgResponse();
    }

当我移除@RequestParam时,错误信息

请求方法 'POST' 不被支持

就消失了。

祝好运。


1
似乎在您的情况下,您应该使用“@RequestBody”。 - Maayan Hope
如果您从方法输入参数中删除了“requestMessage”的@RequestParam,那么您将如何在方法内部访问它? - Ezani

1
只需遵循此步骤。
@Controller
@RequestMapping(
        method={RequestMethod.POST,RequestMethod.GET,RequestMethod....} 
    )

public class YourController{
.
.
.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接