java spring mvc json数据绑定自定义json响应

java spring mvc json数据绑定自定义json响应,java,json,spring,hibernate,rest,Java,Json,Spring,Hibernate,Rest,所以我正在做学生测试系统,这是我的第一个restful web服务,我需要去掉JSON响应中的信息,我不需要发送响应。我知道我可以使用像@JsonIgnore这样的注释,但它不适合我的情况,因为我需要的数据以不同的方法与同一对象区分开来 我知道有映射器和混合,但我认为它不适合这里,因为我只需要从嵌套对象中排除一个属性,而不是对象中的整个对象。对于不同响应方法的多个JSON定制,可能有一些简单的解决方案 我使用的是jackson数据绑定,spring数据,springMVC以及hibernate

所以我正在做学生测试系统,这是我的第一个restful web服务,我需要去掉JSON响应中的信息,我不需要发送响应。我知道我可以使用像
@JsonIgnore
这样的注释,但它不适合我的情况,因为我需要的数据以不同的方法与同一对象区分开来

我知道有映射器和混合,但我认为它不适合这里,因为我只需要从嵌套对象中排除一个属性,而不是对象中的整个对象。对于不同响应方法的多个JSON定制,可能有一些简单的解决方案

我使用的是
jackson数据绑定
spring数据
,spring
MVC
以及
hibernate

我只需要从
testObj
中选择
testName
,但是我可以排除
testOptionsList
?不使用
@JsonIgnore

或者可能是我的总体API架构缺陷

控制器:

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    ScoresByTestDao scoreRep;

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public List<ScoreByTest> getScoresListByUserId(@PathVariable int id) {
        return scoreRep.getTestScoresByUser(id);
    }
我想在UI中填写什么表格:

+------+------+-------+
|Test  |Date  |Score  | 
+------+------+-------+
ScoreByTest类:

@Entity
@Table(name = "scores_by_test")
public class ScoreByTest implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@NotNull
@Column(name = "score")
private float score;

@JoinColumn(name = "id_given_tests", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private GivenTest givenTestObj;

@JoinColumn(name = "id_user", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.EAGER)

private User userObj;

public ScoreByTest() {
}

public ScoreByTest(Integer id) {
    this.id = id;
}

public ScoreByTest(Integer id, float score) {
    this.id = id;
    this.score = score;
}

public Integer getId() {
    return id;
}

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

public float getScore() {
    return score;
}

public void setScore(float score) {
    this.score = score;
}

public GivenTest getGivenTestObj() {
    return givenTestObj;
}

public void setGivenTestsObj(GivenTest givenTestsObj) {
    this.givenTestObj = givenTestsObj;
}

public User getUserObj() {
    return userObj;
}

public void setUserObj(User userObj) {
    this.userObj = userObj;
}
@Entity
@Table(name = "given_tests")
public class GivenTest implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Column(name = "date")
    @Temporal(TemporalType.DATE)
    private Date date = new Date();

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "code")
    private String code;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "givenTestObj", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @JsonIgnore
    private List<ChosenOption> chosenOptionsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "givenTestObj", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @JsonIgnore
    private List<ScoreByTest> scoresByTestList;

    @JoinColumn(name = "id_test", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Test testObj;

    public GivenTest() {
    }

    public GivenTest(Integer id) {
        this.id = id;
    }

    public GivenTest(Integer id, Date date, String code) {
        this.id = id;
        this.date = date;
        this.code = code;
    }

    public Integer getId() {
        return id;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<ChosenOption> getChosenOptionsList() {
        return chosenOptionsList;
    }

    public void setChosenOptionsList(List<ChosenOption> chosenOptionsList) {
        this.chosenOptionsList = chosenOptionsList;
    }

    public List<ScoreByTest> getScoresByTestList() {
        return scoresByTestList;
    }

    public void setScoresByTestList(List<ScoreByTest> scoresByTestList) {
        this.scoresByTestList = scoresByTestList;
    }

    public Test getTestObj() {
        return testObj;
    }

    public void setTestObj(Test testObj) {
        this.testObj = testObj;
    }
GivenTest类:

@Entity
@Table(name = "scores_by_test")
public class ScoreByTest implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@NotNull
@Column(name = "score")
private float score;

@JoinColumn(name = "id_given_tests", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private GivenTest givenTestObj;

@JoinColumn(name = "id_user", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.EAGER)

private User userObj;

public ScoreByTest() {
}

public ScoreByTest(Integer id) {
    this.id = id;
}

public ScoreByTest(Integer id, float score) {
    this.id = id;
    this.score = score;
}

public Integer getId() {
    return id;
}

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

public float getScore() {
    return score;
}

public void setScore(float score) {
    this.score = score;
}

public GivenTest getGivenTestObj() {
    return givenTestObj;
}

public void setGivenTestsObj(GivenTest givenTestsObj) {
    this.givenTestObj = givenTestsObj;
}

public User getUserObj() {
    return userObj;
}

public void setUserObj(User userObj) {
    this.userObj = userObj;
}
@Entity
@Table(name = "given_tests")
public class GivenTest implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @NotNull
    @Column(name = "date")
    @Temporal(TemporalType.DATE)
    private Date date = new Date();

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "code")
    private String code;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "givenTestObj", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @JsonIgnore
    private List<ChosenOption> chosenOptionsList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "givenTestObj", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @JsonIgnore
    private List<ScoreByTest> scoresByTestList;

    @JoinColumn(name = "id_test", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Test testObj;

    public GivenTest() {
    }

    public GivenTest(Integer id) {
        this.id = id;
    }

    public GivenTest(Integer id, Date date, String code) {
        this.id = id;
        this.date = date;
        this.code = code;
    }

    public Integer getId() {
        return id;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public List<ChosenOption> getChosenOptionsList() {
        return chosenOptionsList;
    }

    public void setChosenOptionsList(List<ChosenOption> chosenOptionsList) {
        this.chosenOptionsList = chosenOptionsList;
    }

    public List<ScoreByTest> getScoresByTestList() {
        return scoresByTestList;
    }

    public void setScoresByTestList(List<ScoreByTest> scoresByTestList) {
        this.scoresByTestList = scoresByTestList;
    }

    public Test getTestObj() {
        return testObj;
    }

    public void setTestObj(Test testObj) {
        this.testObj = testObj;
    }
@实体
@表(name=“给定测试”)
公共类GivenTest实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@列(name=“date”)
@时态(TemporalType.DATE)
私有日期=新日期();
@基本(可选=假)
@NotNull
@尺寸(最小值=1,最大值=50)
@列(name=“code”)
私有字符串码;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“givenTestObj”,fetch=FetchType.EAGER)
@获取(值=获取模式。选择)
@杰索尼奥雷
私人列表选择选项列表;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“givenTestObj”,fetch=FetchType.EAGER)
@获取(值=获取模式。选择)
@杰索尼奥雷
私人名单计分表;
@JoinColumn(name=“id\u test”,referencedColumnName=“id”)
@ManyToOne(可选=false,fetch=FetchType.EAGER)
私有测试对象;
公共赠予测试(){
}
公共给定测试(整数id){
this.id=id;
}
公共给定测试(整数id、日期、字符串代码){
this.id=id;
this.date=日期;
this.code=代码;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共日期getDate(){
返回日期;
}
公共作废设置日期(日期){
this.date=日期;
}
公共字符串getCode(){
返回码;
}
公共无效设置码(字符串码){
this.code=代码;
}
公共列表GetChosenOptions列表(){
返回chosenOptionsList;
}
public void setChosenOptions列表(列表ChosenOptions列表){
this.chosenOptionsList=chosenOptionsList;
}
公共列表getScoresByTestList(){
返回记分系统列表;
}
公共无效设置CoresByTestList(列表分数ByTestList){
this.scoresByTestList=scoresByTestList;
}
公共测试getTestObj(){
返回testObj;
}
公共无效设置STOBJ(测试对象){
this.testObj=testObj;
}
测试问题类:

@Entity
@Table(name = "test_questions")
public class TestQuestion implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")

    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Lob
    @Size(min = 1, max = 65535)
    @Column(name = "question")

    private String question;
    @Basic(optional = false)
    @NotNull
    @Column(name = "right_choices")
    private short rightChoices;

    @JoinColumn(name = "id_test", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JsonIgnore
    private Test testObj;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "testQuestionObj", fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    private List<TestOption> testOptionsList;

    public TestQuestion() {
    }

    public TestQuestion(Integer id) {
        this.id = id;
    }

    public TestQuestion(Integer id, String question, short rightChoices) {
        this.id = id;
        this.question = question;
        this.rightChoices = rightChoices;
    }

    public Integer getId() {
        return id;
    }

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

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public short getRightChoices() {
        return rightChoices;
    }

    public void setRightChoices(short rightChoices) {
        this.rightChoices = rightChoices;
    }

    public Test getTestObj() {
        return testObj;
    }

    public void setTestObj(Test testObj) {
        this.testObj = testObj;
    }

    public List<TestOption> getTestOptionsList() {
        return testOptionsList;
    }

    public void setTestOptionsList(List<TestOption> testOptionsList) {
        this.testOptionsList = testOptionsList;
    }
@实体
@表(name=“测试问题”)
公共类TestQuestion实现了可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@高球
@尺寸(最小值=1,最大值=65535)
@列(name=“问题”)
私有字符串问题;
@基本(可选=假)
@NotNull
@列(name=“right\u choices”)
私人短期选择;
@JoinColumn(name=“id\u test”,referencedColumnName=“id”)
@ManyToOne(可选=false,fetch=FetchType.EAGER)
@杰索尼奥雷
私有测试对象;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“testQuestionObj”,fetch=FetchType.EAGER)
@获取(值=获取模式。选择)
私有列表测试选项列表;
公共测试问题(){
}
公共测试问题(整数id){
this.id=id;
}
公共测试问题(整数id、字符串问题、短右选项){
this.id=id;
这个问题=问题;
this.rightChoices=rightChoices;
}
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getQuestion(){
返回问题;
}
公共问题(字符串问题){
这个问题=问题;
}
公共短getRightChoices(){
返回正确的选择;
}
公共无效setRightChoices(短rightChoices){
this.rightChoices=rightChoices;
}
公共测试getTestObj(){
返回testObj;
}
公共无效设置STOBJ(测试对象){
this.testObj=testObj;
}
公共列表GetTestOptions列表(){
返回测试选项列表;
}
公共void settostoptionslist(列表测试选项列表){
this.testOptionsList=testOptionsList;
}
TestOption类:

@Entity
@Table(name = "test_options")
public class TestOption implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;

@Basic(optional = false)
@NotNull
@Column(name = "test_option")
private String testOption;

@Basic(optional = false)
@NotNull
@Column(name = "correct")
private boolean correct;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "testOptionObj", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SELECT)
@JsonIgnore
private List<ChosenOption> chosenOptionsList;

@JoinColumn(name = "id_test_question", referencedColumnName = "id")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JsonIgnore
private TestQuestion testQuestionObj;

public TestOption() {
}

public TestOption(Integer id) {
    this.id = id;
}

public TestOption(Integer id, String testOption, boolean correct) {
    this.id = id;
    this.testOption = testOption;
    this.correct = correct;
}

public Integer getId() {
    return id;
}

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

public String getTestOption() {
    return testOption;
}

public void setTestOption(String testOption) {
    this.testOption = testOption;
}

public boolean getCorrect() {
    return correct;
}

public void setCorrect(boolean correct) {
    this.correct = correct;
}

public List<ChosenOption> getChosenOptionsList() {
    return chosenOptionsList;
}

public void setChosenOptionsList(List<ChosenOption> chosenOptionsList) {
    this.chosenOptionsList = chosenOptionsList;
}

public TestQuestion getTestQuestionObj() {
    return testQuestionObj;
}

public void setTestQuestionObj(TestQuestion testQuestionObj) {
    this.testQuestionObj = testQuestionObj;
}
@实体
@表(name=“测试选项”)
公共类TestOption实现可序列化{
私有静态最终长serialVersionUID=1L;
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
@基本(可选=假)
@列(name=“id”)
私有整数id;
@基本(可选=假)
@NotNull
@列(name=“测试选项”)
私有字符串测试选项;
@基本(可选=假)
@NotNull
@列(name=“correct”)
私有布尔正确;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“testOptionObj”,fetch=FetchType.EAGER)
@获取(值=获取模式。选择)
@杰索尼奥雷
私人列表选择选项列表;
@JoinColumn(name=“id