java - 为什么 Java Spring v.2.2.7 不支持请求方法 'POST'?

标签 java html spring spring-boot thymeleaf

我正在使用 Spring boot v.2.2.7 来实现 post http 方法的休息服务。它在 Postman 中工作正常,但在使用浏览器时显示下一个错误:

There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'POST' not supported

我的 Controller :

@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";
        }

注意:Get 方法工作正常。

我的存储库:

@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>

问题似乎出在 thymeleaf 上。提交表单时 Spring 日志显示:

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

我们可以看到使用了:POST "/addMusicalStyle.html",parameters={} 当它应该是这样的时候: POST "/musicstyle",parameters={name="Jazz",description="From EEUU"} 所以 thymeleaf 没有完成他的工作。

我的pom.xml

<?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>

这就像 th:action、th:object 和 th:field 没有采用正确的参数( postman 客户端的服务正常)。

我尝试过 Spring 2.3.0、2.2.7 和 2.1.14 版本,结果是一样的。

如何解决?

谢谢

最佳答案

我回复我:

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

关于java - 为什么 Java Spring v.2.2.7 不支持请求方法 'POST'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61876635/

相关文章:

java - 删除重复代码的模式

Java从文件读取到数组运行时错误

java - 8 之前的 Java 版本怎么会不支持闭包呢?

html - 外部 div 背景与内部 div 重叠

javascript - 在使用 jQuery .animate() + ie8 兼容性创建交叉渐变图像库时遇到问题

spring - 覆盖审核字段@CreatedDate

java - 在常量池中看不到整数值

html - 绝对定位按钮未定位在其父 DIV 中

java - 有 header 时请求映射不起作用

java - SpringFox 依赖破坏了我的 Spring 上下文