将数据从表单传递到REST端点

时间:2017-09-28 21:45:37

标签: java spring rest

我对编写REST端点有点新意,因此这个问题。 我正在编写一个REST端点,它应该支持在iOS中注册用户的表单提交。

这是我的用户类。

@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long Id;
  private String username;
  private String email;
  private String password;
}

这是我被要求使用的控制器用户注册签名,

@RestController
public class RegistrationController {

  @RequestMapping(value = "/user/signup",
                  method = RequestMethod.POST,
                  consumes = {"application/json"})
  public ResponseEntity<?> showRegistrationForm(@RequestBody User user) {
    try{
      //persist the User
      return new ResponseEntity(HttpStatus.OK);

    }catch(Exception e){

    }

    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
}

使用以下JSON有效内容

向我提供了用户对象
{
  "username": "Something",
  "email": "Something",
  "password": "password"
}

我不明白的是如何抓取表单中发布的字段并将其转换为POST请求中的用户。我不需要将参数更改为字符串,验证它们然后组成User对象。或者正是这样做的正确方法。 任何帮助表示赞赏。

0 个答案:

没有答案