当前位置: 首页 > 知识库问答 >
问题:

HTTP状态405-不支持请求方法“PUT”

薛元忠
2023-03-14

我有以下控制器:

@RestController
public class RestaurantController {
    @Autowired
    RestaurantService restaurantService;
    @RequestMapping(value = "/restaurant/", method = RequestMethod.GET)
    public ResponseEntity<List<Restaurant>> listAllRestaurants() {
        System.out.println("Fetching all restaurants");
        List<Restaurant> restaurants = restaurantService.findAllRestaurants();
        if(restaurants.isEmpty()){
            return new ResponseEntity<List<Restaurant>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
        }
        return new ResponseEntity<List<Restaurant>>(restaurants, HttpStatus.OK);
    }
    @RequestMapping(value = "/restaurant/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Restaurant> updateRestaurant(@PathVariable("id") int id, @RequestBody Restaurant restaurant) {
        System.out.println("Updating Restaurant " + id);

        Restaurant currentRestaurant = restaurantService.findById(id);

        if (currentRestaurant==null) {
            System.out.println("Restaurant with id " + id + " not found");
            return new ResponseEntity<Restaurant>(HttpStatus.NOT_FOUND);
        }

        currentRestaurant.setName(restaurant.getName());
        currentRestaurant.setDescription(restaurant.getDescription());
        currentRestaurant.setIcon(restaurant.getIcon());

        restaurantService.updateRestaurant(currentRestaurant);
        return new ResponseEntity<Restaurant>(currentRestaurant, HttpStatus.OK);
    }
}

13-feb-2016 16:55:09.442警告[http-apr-8080-exec-9]org.springframework.web.servlet.PageNotFound.HandleHttpRequestMethodNotSupported请求方法“Put”不受支持

我不明白这怎么可能。下面是我的依赖项:

<properties>
        <springframework.version>4.1.6.RELEASE</springframework.version>
        <springsecurity.version>4.0.1.RELEASE</springsecurity.version>
        <hibernate.version>4.3.6.Final</hibernate.version>
        <mysql.connector.version>5.1.31</mysql.connector.version>
        <jackson.version>2.6.3</jackson.version>
        <joda-time.version>2.3</joda-time.version>
        <testng.version>6.9.4</testng.version>
        <mockito.version>1.10.19</mockito.version>
        <h2.version>1.4.187</h2.version>
        <dbunit.version>2.2</dbunit.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- jsr303 validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.1.3.Final</version>
        </dependency>

        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>

        <!-- Joda-Time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda-time.version}</version>
        </dependency>

        <!-- To map JodaTime with database type -->
        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.core</artifactId>
            <version>3.0.0.CR1</version>
        </dependency>
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${springsecurity.version}</version>
        </dependency>
        <!-- Servlet+JSP+JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Need this for json to/from object -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- Testing dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>${dbunit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {


    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("bill").password("user").roles("USER");
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
        auth.inMemoryAuthentication().withUser("dba").password("dba").roles("ADMIN","DBA");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

      http.authorizeRequests()
        .antMatchers("/", "/home","/restaurant/**").permitAll()
        .antMatchers("/list").access("hasRole('USER')")
        .antMatchers("/list").access("hasRole('ADMIN')")
        .antMatchers("/admin/**").access("hasRole('ADMIN')")
        .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
        .and().formLogin().loginPage("/login")
        .usernameParameter("ssoId").passwordParameter("password")
        .and().csrf()
        .and().exceptionHandling().accessDeniedPage("/Access_Denied");
    }
}

编辑2:

2016-02-14 12:30:56 DEBUG FilterChainProxy:324-/Restaurant/1位于附加筛选器链中12个位置1;正在激发筛选器:“WebAsyncManagerIntegrationFilter”

2016-02-14 12:30:56 DEBUG FilterChainProxy:324-/Restaurant/1位于附加筛选器链中12的位置2;正在激发筛选器:“SecurityContextPersistenceFilter”

2016-02-14 12:30:56 DEBUG httpsessionSecurityContextRepository:101-从HttpSession:null中没有可用的SecurityContext。将创建一个新的。

2016-02-14 12:30:56 DEBUG FilterChainProxy:324-/Restaurant/1位于附加过滤器链中12个位置3;正在激发筛选器:“HeaderWriterFilter”

2016-02-14 12:30:56 DEBUG hstsheaderwriter:128-未注入HSTS标头,因为它与requestMatcher org.springframework.security.web.header.writers.hstsheaderwriter$SecureRequestmatcher@3DED3D8A不匹配

2016-02-14 12:30:56 DEBUG FilterChainProxy:324-/Restaurant/1位于附加过滤器链中12个位置4;激发筛选器:“CSRFFILTER”

2016-02-14 12:30:56 DEBUG csrffilter:106-发现http://localhost:8080/SpringSecurityCusotmloginforMannotationExample/Restaurant/1的CSRF令牌无效

2016-02-14 12:30:56 DEBUG DispatcherServlet:861-名为“dispatcher”的DispatcherServlet正在处理[/SpringSecurityCusotmmLoginforMannotationExample/Access_Denied]的PUT请求

2016-02-14 12:30:56 DEBUG RequestMappingHandlerMapping:294-查找path/ACCESS_DENIED的处理程序方法

2016-02-14 12:30:56 DEBUG ExceptionHandlerExceptionResolver:134-解析处理程序的异常[null]:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“Put”

2016-02-14 12:30:56 DEBUG ResponseStatusExceptionResolver:134-解析处理程序[null]:org.springFramework.web.HttpRequestMethodNotSupportedException:不支持请求方法“Put”

2016-02-14 12:30:56 DEBUG DefaultHandlerExceptionResolver:134-从处理程序[null]:org.springframework.web.HttpRequestMethodNotSupportedException:不支持请求方法“Put”

2016-02-14 12:30:56警告PageNotFound:198-不支持请求方法“Put”

2016-02-14 12:30:56调试httpsessionSecurityContextRepository:337-SecurityContext为空或内容为匿名-上下文不会存储在HttpSession中。

2016-02-14 12:30:56 DEBUG DispatcherServlet:1034-Null ModelAndView返回到名为“dispatcher”的DispatcherServlet:假设HandlerAdapter已完成请求处理

2016-02-14 12:30:56 DEBUG DispatcherServlet:996-成功完成请求

2016-02-14 12:30:56 DEBUG defaultlistableBeanFactory:248-返回单个bean的缓存实例“delegating applicationlistener”

2016-02-14 12:30:56调试httpsessionSecurityContextRepository:337-SecurityContext为空或内容为匿名-上下文不会存储在HttpSession中。

2016-02-14 12:30:56调试SecurityContextPersistenceFilter:105-SecurityContextThOlder现在已清除,因为请求处理已完成

共有1个答案

鲜于海
2023-03-14

尝试将org.springframework.web的日志级别调高为debug。这将使您了解Spring是如何处理请求的。希望它能给你(或我们)一些关于如何修复它的更多线索。

如果您使用的是Spring Boot,只需在application.properties文件中添加这一行:

logging.level.org.springframework.web=DEBUG

在看到其他日志记录后编辑:

 类似资料:
  • 问题内容: 我收到此错误: 我正在尝试做的是创建一个带有下拉框的表单,该表单会根据在另一个下拉框中选择的其他值进行填充。例如,当我在框中选择一个名称时,应运行.jsp页面中的函数,然后提交提交的页面,然后在框中再次加载相应的值。 但是我收到此HTTP状态405错误。我在互联网上搜索了解决方案,但找不到任何有帮助的方法。这是我的代码的相关部分: jsp页面的一部分 控制器的一部分: 我怎么会得到这个

  • 我收到这个错误:< code>HTTP状态405 -不支持请求方法“POST ” 我想做的是创建一个带有下拉框的表单,该下拉框根据在另一个下拉框中选择的其他值进行填充。例如,当我在框中选择一个名称时,应该运行. jsp页面中的函数,然后提交的页面再次加载框中的相应值。 但是我收到此HTTP状态405错误。我已经在互联网上搜索了解决方案,但找不到任何有帮助的东西。以下是我的代码的相关部分: jsp页

  • 我有Spring MVC的Spring Security。当我尝试注册时,它给了我405个不支持的“帖子”。我已在安全配置中禁用csrf令牌。让我知道我哪里出错了? 我的登录页面: 授权由LoginController处理: 这是我的Spring Security配置类:

  • 当我尝试实现Spring Security性时,出现以下错误- 控制器: web.xml Spring-security.xml 登录名。jsp 错误:- http://localhost:8080/EmployeeManagement/j_spring_security_check

  • 该场景是用户选择一些产品,然后单击进行支付。在这里,我将他/她重定向到IPG(银行互联网支付网关),并在付款完成和定稿时传递我的返回url。在我添加spring security之前,一切正常。 但是如果在一些内部视图中发布这个url,一切都会恢复正常。 这是正常工作(spring security启用,一切正常) 在浏览器中查看银行IPG的来源(https://pna.shaparak.ir/C

  • 我对Spring MVC项目有问题,当我尝试在控制器中调用post方法时,我得到“HTTP状态405-请求方法'POST'不支持”。我没有使用Spring安全。返回“index”是base jsp,并基于“view”属性更改视图。有人能找到我做错了什么? 控制器: JSP: 窗体对象: