问题描述
我正在为Spring项目开发Rest API。 为此,我一直在使用泽西岛。
我的控制器如下所示:
@Path("movies")
@Component
public class MovieController {
@Autowired
MoviesService moviesService;
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getAll(@QueryParam("count") int rowsPerPage, @QueryParam("page") int page){
PaginationResponse<List<Movie>> response = moviesService.getList(page,rowsPerPage);
if(response==null){
return Response.noContent().build();
}
return Response.ok(response).build();
}
}
但是,调用此方法时,返回如下。
{"content":["ar.edu.itba.paw2018b.models.Movie@1f6c00ef"],"hasNext":true,"hasPrevious":false}
出于某种原因,电影POJO似乎是通过调用Object的toString()方法进行序列化的,该方法显然不是电影对象的JSON表示形式。
以下是Movie和PaginationResponse类的摘录。
电影课
@Entity
@Table(name = "movies")
public class Movie {
@Column(name = "image")
private byte[] img;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(sequenceName = "movies_movieid_seq", name = "movies_movieid_seq", allocationSize = 1)
@Column(name = "movieid", nullable = false)
private long id;
@Column(name = "title")
private String name;
@Column
private double rating;
@Column(name = "releasedate", columnDefinition="DATE")
private Date releaseDate;
@Column
private int runtime;
@Column
private String genres;
@Column
private boolean active;
@Column
private String trailer;
@Column
private String summary;
@Column
private String director;
@Column
private String actors;
@Column
private String restriction;
@JsonIgnore
@OneToMany(mappedBy = "movie",fetch = FetchType.EAGER, orphanRemoval = true)
private List<Screening> screenings;
public Movie(){}
public Movie(long id, String name, float rating, Date releaseDate, int runtime, String genres, byte[] img, String trailer,
String summary, String director, String actors, String restriction){
this.id = id;
this.name = name;
this.rating = rating;
this.releaseDate = releaseDate;
this.runtime = runtime;
this.genres = genres;
this.img = img;
this.active = true;
this.trailer= trailer;
this.summary = summary;
this.director = director;
this.actors = actors;
this.restriction = restriction;
}
//Getters and Setters for all properties
}
分页响应类
public class PaginationResponse<T extends List> {
private boolean hasNext;
private boolean hasPrevious;
private T content;
public interface Paginageable<T>{
T getData(int querySize, int queryStart);
}
public PaginationResponse(int page, int rowsPerPage, Paginageable<T> paginageable)
{
fetchPage(page, rowsPerPage, paginageable);
}
public PaginationResponse(){}
public PaginationResponse(boolean hasNext, boolean hasPrevious, T content) {
this.hasNext = hasNext;
this.hasPrevious = hasPrevious;
this.content = content;
}
public void fetchPage(int page, int rowsPerPage, Paginageable<T> paginageable)
{
int start = page*rowsPerPage;
boolean hasPrevious=true;
boolean hasNext = false;
if(start<0){
this.hasNext=true;
this.hasPrevious=false;
this.content=null;
return;
}
int queryStart = start -1;
int querySize = rowsPerPage + 2;
if(queryStart<0){
hasPrevious=false;
queryStart = start;
querySize = rowsPerPage +1;
}
T content = paginageable.getData(querySize, queryStart);
if(content.size()==querySize){
hasNext=true;
content.remove(content.size()-1);
}
if(hasPrevious){
if(content.isEmpty())
{
hasPrevious=false;
}
else{
content.remove(0);
}
}
this.hasNext=hasNext;
this.hasPrevious=hasPrevious;
this.content=content;
}
//Getters and Setters for all properties
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="paw2018b"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>TicketCentral</display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
ar.edu.itba.paw2018b.webapp.config.WebConfig,
ar.edu.itba.paw2018b.webapp.config.WebAuthConfig,
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>ar.edu.itba.paw2018b.webapp.controller</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>ROOT</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>ROOT</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/404</location>
</error-page>
<error-page>
<location>/error</location>
</error-page>
</web-app>
任何帮助将不胜感激,谢谢。
1楼
尝试在Movie类中重写toString()方法,然后您的响应json将正确形成。