返回的如果是java实体,前端收到的是JSon,当返回的实体放进response中时,前端收到的就是对象而不是Json了,这是怎么回事,我需要传JSon
下面贴resource代码
@Path("/User")
public class UserResource
{
@GET
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public StudentEntity query(){
StudentEntity studentEntity = new StudentEntity();
BookEntity bookEntity = new BookEntity();
bookEntity.setId((long) 12321);
bookEntity.setAge(18);
bookEntity.setName("王二小");
studentEntity.setId((long) 12321);
studentEntity.setAge(18);
studentEntity.setName("王二小");
studentEntity.setBook(bookEntity);
return studentEntity;
}
}
@Path("/User")
public class UserResource
{
@GET
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response query(){
StudentEntity studentEntity = new StudentEntity();
BookEntity bookEntity = new BookEntity();
bookEntity.setId((long) 12321);
bookEntity.setAge(18);
bookEntity.setName("王二小");
studentEntity.setId((long) 12321);
studentEntity.setAge(18);
studentEntity.setName("王二小");
studentEntity.setBook(bookEntity);
return ResponseBuilder.buildSuccessResponse(studentEntity);
}
}
然后是自定义response
public class BaseResponse implements Serializable{
private static final long serialVersionUID = 8647598317829542864L;
private Boolean success = true;
private String message;
private Object result;
private PagerResultInfo result_info;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
public PagerResultInfo getResult_info() {
return result_info;
}
public void setResult_info(PagerResultInfo result_info) {
this.result_info = result_info;
}
}
public class ResponseBuilder {
public static Response buildSuccessResponse(Object obj) {
if (obj instanceof Pager) { return buildPager((Pager) obj); }
BaseResponse response = new BaseResponse();
response.setSuccess(Boolean.valueOf(true));
response.setMessage("操作成功!");
response.setResult(obj);
return Response.status(Response.Status.OK).entity(response).build();
}
}