在spring mvc中输出注册用户为Json

时间:2017-01-06 06:35:10

标签: json spring spring-mvc spring-boot

我有一个简单的表单,用于生成id,然后是标题,用户名和密码。一旦用户注册,我想去Json页面并能够在那里提取数据,这是否可能?如果是这样怎么做?这就是我现在所拥有的,但这不起作用。

型号:

public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;

    @NotNull
    @Size(min=2, max=30)
    private String username;

    @NotNull
    @Size(min=6)
    private String password;

    public String getTitle() { return title; }

    public String getUsername() { return username; }

    public String getPassword() { return password; }

    public Long getId() { return id; }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) { this.username = username; }

    public void setPassword(String password) { this.password = password; }

    public void setTitle(String title) { this.title = title; }

    public List<Note> getNotes() {
        return notes;
    }

    @OneToMany
    private List<Note> notes = new ArrayList<Note>();
}

控制器:

    @RequestMapping(value = "/json", method = RequestMethod.GET)
    @ResponseBody
    public User json(){
        User user = new User();
        user.setId(id);
        user.setTitle(title);
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }
}

0 个答案:

没有答案