reactjs RESTReact表单不工作[Java Spring后端]

qgzx9mmu  于 2022-11-22  发布在  React
关注(0)|答案(1)|浏览(1125)

我的Java Spring后端API工作正常。我可以使用例如curl POST方法创建新用户。但是,当我尝试在浏览器中对表单执行相同操作时,没有用户存储在数据库中。以下是代码:
类App方法创建新用户

// tag::create[]
onCreate(newUser) {
   follow(client, root, ['users']).then(userCollection => {
      return client({
         method: 'POST',
         path: userCollection.entity._links.self.href,
         entity: newUser,
         headers: {'Content-Type': 'application/json'}
      })
   }).then(response => {
      return follow(client, root, [
         {rel: 'users', params: {'size': this.state.pageSize}}]);
   }).done(response => {
      if (typeof response.entity._links.last !== "undefined") {
         this.onNavigate(response.entity._links.last.href);
      } else {
         this.onNavigate(response.entity._links.self.href);
      }
   });
}
// end::create[]

下面是班级创建对话框中填写的数据:

// tag::create-dialog[]
class CreateDialog extends React.Component {

   constructor(props) {
      super(props);
      this.handleSubmit = this.handleSubmit.bind(this);
   }

   handleSubmit(e) {
      e.preventDefault();
      const newUser = {};
      this.props.attributes.forEach(attribute => {
         newUser[attribute] = ReactDOM.findDOMNode(this.refs[attribute]).value.trim();
      });
      this.props.onCreate(newUser);

      // clear out the dialog's inputs
      this.props.attributes.forEach(attribute => {
         ReactDOM.findDOMNode(this.refs[attribute]).value = '';
      });

      // Navigate away from the dialog to hide it.
      window.location = "#";
   }

   render() {
      const inputs = this.props.attributes.map(attribute =>
         <p key={attribute}>
            <input type="text" placeholder={attribute} ref={attribute} className="field"/>
         </p>
      );

      return (
         <div>
            <a href="#createUser">Create</a>

            <div id="createUser" className="modalDialog">
               <div>
                  <a href="#" title="Close" className="close">X</a>

                  <h2>Create new user</h2>

                  <form>
                     {inputs}
                     <button onClick={this.handleSubmit}>Create</button>
                  </form>
               </div>
            </div>
         </div>
      )
   }

}
// end::create-dialog[]

我在这里做错了什么,以至于我的POST查询最终没有创建新用户?
我试图重写方法,使他们将正确地生成新的对象在数据库中的基础上输入来自客户端。

b4lqfgs4

b4lqfgs41#

好的,所以解决这个问题需要我更深入地研究在浏览器中的开发工具中完成的React调试。所以我收到了这样的错误:

"JSON parse error: Cannot coerce empty String ("") to element of `java.util.Set<com.gloss.model.EmailAddress>` (but could if coercion was enabled using `CoercionConfig`); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot coerce empty String ("") to element of `java.util.Set<com.gloss.model.EmailAddress>` (but could if coercion was enabled using `CoercionConfig`)
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 75] (through reference chain: com.gloss.model.User["emailAddress"])"

当我回顾用户模型时,它接收到的字符串无法Map到属于Set数据结构的对象的示例。最简单的解决方案是通过在属性文件中添加以下行来配置Jackson,使其接受空字符串为null:

spring.jackson.deserialization.accept-empty-string-as-null-object=true

这对我很管用。

相关问题