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

为什么在Java Spring V.2.2.7中不支持请求方法“post”?

卫高谊
2023-03-14

我使用Spring boot V.2.2.7来为post http方法实现rest服务。它的工作从邮差是好的,但显示下一个错误时,使用浏览器:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported
@Controller
public class SystemAdministratorController {
    @Autowired
    private StyleRepository styleRepository;

    //Method get, post, put and delete for music styles
    @GetMapping("/musicalstyle")
    public String getStyle(Model model) {
        List<Style> listStyles = styleRepository.findAll();
        model.addAttribute("styles", listStyles);
        return "musicalStyle";
    }

    @PostMapping("/musicalstyle")
    public String addMusicalStyle(@Valid Style style) {
        styleRepository.save(style);
        return "template";
        }
@Repository
public interface StyleRepository extends JpaRepository<Style, Long>{

}
@Entity
@Table(name = "style", schema = "musicnet")
public class Style {

    private String name;
    private String description;

    public Style() {

    }

    public Style(String name, String description) {
        this.name = name;
        this.description = description;
    }

    @Id
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "description", nullable = false)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "SystemAdministrator [name=" + name + ", description=" + description + "]";
    }

我得网站配置:

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

    }

}

我的应用程序

@SpringBootApplication() 
@EnableJpaAuditing
public class MusicnetApplication {

    public static void main(String[] args) {
        SpringApplication.run(MusicnetApplication.class, args);


    }

}

AddMusicalStyle.html(调用post方法的表单):

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>


</head>
<body>

    <form th:action="@{/musicalstyle}" th:object="${style}" method="post">
        <label for="name">Nombre</label>
        <input type="text" th:field="*{name}"  />
        <label for="description">Descripción</label>
        <input type="text" th:field="*{description}" />
        <button type="submit" th:text="Añadir">Añadir</button>
    </form>


</body>
</html>
2020-05-18 20:07:45.793 DEBUG 9752 --- [nio-8182-exec-3] o.s.web.servlet.DispatcherServlet        : POST "/addMusicalStyle.html", parameters={}
2020-05-18 20:07:45.810 DEBUG 9752 --- [nio-8182-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-05-18 20:07:45.836  WARN 9752 --- [nio-8182-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2020-05-18 20:07:45.837 DEBUG 9752 --- [nio-8182-exec-3] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED
2020-05-18 20:07:45.846 DEBUG 9752 --- [nio-8182-exec-3] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for POST "/error", parameters={}
2020-05-18 20:07:45.854 DEBUG 9752 --- [nio-8182-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2020-05-18 20:07:46.408 DEBUG 9752 --- [nio-8182-exec-3] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2020-05-18 20:07:46.432 DEBUG 9752 --- [nio-8182-exec-3] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 405
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.musicnet</groupId>
    <artifactId>musicnet</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>musicnet</name>
    <description>MusicNet is Spring project to develop the TFG</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

谢谢,

共有1个答案

慕高格
2023-03-14

我回应我:

为了让Thymeleaf处理文件,它们应该位于/src/main/java/resources/templates,所以出现了问题。

 类似资料:
  • 我正在编写一个控制器来处理来自AJAX的帖子。我一直收到一个错误,那篇文章不受支持。我以前在尝试创建后控制器方法时从未遇到过以下错误,我希望有人能发现我在哪里搞砸了。 这是我为控制控制器中的帖子而编写的方法: 使用JQuery 1.10,这是请求它的Ajax调用: 我知道POST地址是正确的,因为将它转换成GET请求就可以了。此外,我还编写了其他POST请求,它们在同一个控制器类中也能正常工作。任

  • login.jsp 用户列表.jsp AppController.java 有2页:login.jsp-起始页,其中包括与登录和密码填充的形式-userlist.jsp结果列表“显示数据库中所有用户持久化”..首先显示登录页面,当我单击提交按钮时,我得到了这个错误:org . spring framework . web . servlet . pagenotfound-不支持请求方法“POST”

  • 下面是我的方法 当调用GET request时,它的工作很好,但是当我们调用POST request和request payload checkout_token=xxxx时,我得到以下错误 编辑 甚至我尝试删除所有参数,但仍然没有运气到目前为止。

  • 我刚刚开始学习Java Spring Boot for REST API开发。在下面的代码中,GET方法可以正常工作,但POST不能。 在Postman应用程序中使用as 错误 在日志中我可以看到, maven的Java和springboot版本,

  • 我正在使用Spring Boot和Angular JS构建一个web应用程序,它将执行CRUD操作。我已经成功创建了一个类,在实现这个类时,我遇到了一个错误“请求方法'POST'不受支持” 这是当我用POST请求点击上面的URL时得到的错误。 这是我的前端JS代码,用于点击URL。我在跑步http://localhost:4200/truck努力让它工作。

  • 我的控制器类是: 应用程序类: 我尝试禁用,并在该方法上添加,但似乎没有任何效果。