如何在返回ResponseEntity时自定义jackson的dafault行为<>使用PagedResources<&gt ;?

时间:2017-11-02 15:08:45

标签: spring jackson spring-data spring-rest spring-hateoas

我在我的proyect上使用弹簧靴。基本上,我想要的只是在GET方法的响应中包含内联实体,同时仍然使用spring-hateoas(_embedded和links)的优点。

有这个课程:

@Entity
@Table(name = "sub_specialty", schema = "public", catalog = "icorelab")
public class SubSpecialty {
private Integer id;
private String name;
private Boolean active;
private Date createdAt;
private Date deletedAt;
private Specialty specialty;
private String description;

@ManyToOne
@JoinColumn(name = "specialty_id", referencedColumnName = "id", nullable = false)
public Specialty getSpecialtyBySpecialtyId() {
    return specialty;
}

我使用PagingAndSorting功能设置存储库,还扩展了CustomRepository以提供自定义方法。此存储库使用投影,因此如此处所述,iternal实体被序列化 https://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.excerpts

 @RepositoryRestResource(collectionResourceRel = "sub_specialties", path =    "sub_specialties",
    excerptProjection = InlineSpecialty.class)
 public interface SubSpecialtyRepository extends    PagingAndSortingRepository<SubSpecialty, Integer>,SubSpecialtyRepositoryCustom {

在控制器中,我使用Page和PageResourcesAssembler

返回响应
Page<SubSpecialty> page =  subSpecialtyRepository.filter(name,description,active,specialty,limit,offset,pageable.getSort());
    PagedResources<SubSpecialty> pagedResources = pagedResourcesAssembler.toResource(page,subSpecialtyResourceAssembler);


    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/sub_specialties/filter", offset, limit);
    ResponseEntity<?> response = new ResponseEntity<>(pagedResources, headers, HttpStatus.OK);
    return response;

问题是该消息不包括专业信息,只包括链接

{
    "_embedded": {
        "sub_specialties": [
            {
                "id": 107,
                "name": "PHARMACOLOGY",
                "active": false,
                "createdAt": "2017-09-30",
                "deletedAt": null,
                "description": null,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sub_specialties/107{?projection}",
                        "templated": true
                    },
                    "subSpecialty": {
                        "href": "http://localhost:8080/sub_specialties/107{?projection}",
                        "templated": true
                    },
                    "specialty": {
                        "href": "http://localhost:8080/specialties/16"
                    }
                }
            },
            {
                "id": 104,
                "name": "PHARMACOLOGY",
                "active": false,
                "createdAt": "2017-09-30",
                "deletedAt": null,
                "description": null,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sub_specialties/104{?projection}",
                        "templated": true
                    },
                    "subSpecialty": {
                        "href": "http://localhost:8080/sub_specialties/104{?projection}",
                        "templated": true
                    },
                    "specialty": {
                        "href": "http://localhost:8080/specialties/16"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/sub_specialties/filter?page=0&size=20"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 2,
        "totalPages": 1,
        "number": 0
    }
}

反正有没有实现这个目标?我以为我可以在实体域类或自定义jackson序列化器上使用注释.Actribute实体包含在响应中,但不包含在接收客户端的消息中。我想jackson有一些默认配置可以检测出Specialty类型的存储库,并且只包含响应中的链接。

0 个答案:

没有答案