Spring + Jackson - @JsonView没有返回对象

时间:2017-05-02 14:30:00

标签: json spring spring-mvc jackson

我和杰克逊有一个Spring启动项目。

我将此控制器注释为@JsonView(Views.MobileAppView.class)

@RequestMapping(value = "/reservations")
@JsonView(Views.MobileAppView.class)
public ResponseEntity<Iterable<Reservation>> getReservationList() {
        User user = authenticationfacade.getAuthenticatedUser();
        Iterable<Reservation> reservations = service.findAllByUserAndByOrderByFromDateDesc(user);

        return new ResponseEntity<Iterable<Reservation>>(reservations, HttpStatus.OK);
    }

我的预订对象是

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonView(Views.MobileAppView.class)
    private Long reservationId;

    @ManyToOne(cascade= CascadeType.ALL)
    @JoinColumn(name = "userId")
    @JsonBackReference(value="reservations-user-reference")
    private User user;

    @ManyToOne(cascade= CascadeType.ALL)
    @JoinColumn(name="plateId")
    @JsonView(Views.MobileAppView.class)
    @JsonBackReference(value="reservations-plate-reference")
    private Plate plate;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd/MM/yyyy HH:mm")
    @JsonView(Views.MobileAppView.class)
    private Date fromDate;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern="dd/MM/yyyy HH:mm")
    @JsonView(Views.MobileAppView.class)
    private Date toDate;

    @Temporal(TemporalType.TIMESTAMP)
    @JsonSerialize(using = CustomDateTimeSerializer.class)
    private Date requestDate;

    @JsonView(Views.MobileAppView.class)
    private int price;

    @JsonBackReference(value="reservations-park-reference")
    @ManyToOne(cascade= CascadeType.ALL)
    @JoinColumn(name = "parkId")
    @JsonView(Views.MobileAppView.class)
    private Park park;

    @JsonView(Views.MobileAppView.class)
    private String status;

发送请求时,我只是获取了我注释的所有字段,ParkPlate除外...

[
  {
    "reservationId": 111,
    "fromDate": "08-06-2017 16:30:00",
    "toDate": "09-06-2017 16:30:00",
    "price": 0,
    "status": "Active"
  },
  {
    "reservationId": 220,
    "fromDate": "09-05-2017 10:45:00",
    "toDate": "16-05-2017 11:00:00",
    "price": 0,
    "status": "Active"
  },

这是Plate.class(类似的Park类)

@Entity
public class Plate {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonView(Views.MobileAppView.class)
    private Long plateId;

    @JsonView(Views.MobileAppView.class)
    private String plateNumber;

    @JsonView(Views.MobileAppView.class)
    private String plateName;

    @ManyToOne
    @JoinColumn(name="userId")
    @JsonBackReference(value="plates-user-reference")
    private User user;

    private boolean favourite;

    @JsonIgnore
    private boolean enabled;

这是视图类

public class Views {


    public static class UserView{

    }

    public static class MobileAppView{

    }
}

我试着用@JsonView注释Park和Plate字段,没有运气......

0 个答案:

没有答案