使用 Spring 获取表单数据时出现问题

Bob*_*sq. 2 html java spring spring-mvc

我对前端开发非常陌生。我有一个简单的 Web 应用程序,您可以在其中提交表单,并且我希望能够在 REST 端点中获取表单数据。我正在使用包含 Spring MVC 的 Spring boot。

HTML:

<div class="modal-header modal-header-info">
    <button type="button" class="close" data-dismiss="modal"
        aria-hidden="true">&times;</button>
    <h4 class="modal-title">Add Entity</h4>
</div>
<div class="modal-body">
    <form role="form" method="post" action="/createEntity">
        <div class="form-group">
            <label for="name">Name:</label> <input type="text"
                class="form-control" id="name">
        </div>
        <button type="submit" class="btn btn-info">Submit</button>
    </form>
</div> 
Run Code Online (Sandbox Code Playgroud)

爪哇:

@RequestMapping(value = "/createEntity", method = RequestMethod.POST)
public void createEntity(@RequestBody String payload) {
    System.out.println("Hello world!");
    System.out.println(payload);
}
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

Failed to read HTTP message:
           org.springframework.http.converter.HttpMessageNotReadableException: 
           Required request body is missing: public void    main.java.info.spring.data.neo4j.controllers.CreateEntityController.createEntity(java.lang.String)
Run Code Online (Sandbox Code Playgroud)

如何从表单中获取“名称”值?如果我取出 @RequestBody 部分,则会打印出“hello world”属性。

编辑:我的每个请求的配置:

@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = { "main.java.info.spring.data.neo4j.services",
        "main.java.info.spring.data.neo4j.controllers" })
@Configuration
@EnableNeo4jRepositories(basePackages = "main.java.info.spring.data.neo4j.repositories")
public class MyNeo4jConfiguration extends Neo4jConfiguration {

    public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL")
            : "http://localhost:7474";

    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer(URL, "neo4j", "LOL my pass");

    }

    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("main.java.info.spring.data.neo4j.domain");
    }
}
Run Code Online (Sandbox Code Playgroud)

man*_*ish 5

问题 1:HTML 表单控件缺少name属性

HTML 表单以名称-值对的形式提交。没有name属性的表单控件无法提交,因为它们的名称未知。该id属性仅用于引用客户端的控件。

<input type="text" class="form-control" id="name">
Run Code Online (Sandbox Code Playgroud)

缺少该name属性。它应该是:

<input type="text" class="form-control" id="name" name="name"/>
Run Code Online (Sandbox Code Playgroud)

这会将控件作为 HTTP 请求正文中的表单参数提交,如下所示:

name: [the value you specify for the control]
Run Code Online (Sandbox Code Playgroud)

问题 2@RequestBody作为控制器方法参数将为您提供整个 HTTP 请求正文

public void createEntity(@RequestBody String payload)
Run Code Online (Sandbox Code Playgroud)

意味着您希望将整个 HTTP 请求正文作为 a 传递String给控制器​​方法createEntity。如果您的 HTML 表单确实是您所发布的内容,您将获得以下值payload(在解决问题 1 后):

name: [the value you specify for the control]
Run Code Online (Sandbox Code Playgroud)

如果这是您想要的,请保留@RequestBody注释,但我怀疑您只对请求参数的值感兴趣name。如果是这种情况,您需要将方法声明更改为(在解决问题 1 之后):

public void createEntity(@RequestParam String name)
Run Code Online (Sandbox Code Playgroud)

这会将 HTTP 请求参数映射name到控制器方法参数name

问题 3:作为void控制器方法的返回类型将强制 Spring MVC 查找与方法名称同名的视图

由于您将控制器方法声明为:

public void createEntity(@RequestBody String payload)
Run Code Online (Sandbox Code Playgroud)

Spring MVC 将查找createEntity在此方法退出后命名的视图。如果您有一个具有该名称的视图,那就太好了。否则,一旦解决了问题 1 和 2,您就会收到404 - Resource not found错误。