Spring / JPA / ReST控制器 - 返回对象列表

时间:2018-06-06 17:14:57

标签: java spring rest jpa

我有一个Spring Boot项目,其中Rest控制器有一个返回由实体列表组成的DTO的方法。

@RestController
public class PersonnelController {

 @RequestMapping(value = "/personnel/GetByFirstNameOrLastNameOrSSN/{paramValue}", method = RequestMethod.GET)

public ResponseEntity<GetPageablePersonnelOutput> FindByLastNameOrFirstNameOrSSN  (@PathVariable String paramValue) {

        GetPageablePersonnelByLastNameOrFirstNameOrSSNInput gu = new GetPageablePersonnelByLastNameOrFirstNameOrSSNInput();

        gu.setLastNameOrFirstNameOrSSN(paramValue);

        GetPageablePersonnelOutput go = personnelAppService.FindByLastNameOrFirstNameOrSSN(gu);

        if(go == null)
        {
            return new ResponseEntity(new EmptyJsonResponse(), HttpStatus.OK);
        }

        ResponseEntity rs =  new ResponseEntity(go, HttpStatus.OK);

        return rs;
    }
}

DTO的代码如下:

package com.stsi.pss.application.Model.Dto;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.stsi.pss.domain.Model.PersonnelDetailsEntity;
import com.stsi.pss.domain.Model.PersonnelEntity;

import java.util.List;
import java.util.Objects;
import java.util.Set;

public class GetPageablePersonnelOutput {
    private List<PersonnelEntity> personnel;
    public List<PersonnelEntity> getPersonnel() {
        return personnel;
    }
    public void setPersonnel(List<PersonnelEntity> personnel) {
        this.personnel = personnel;
    }
}

PersonnelEntity的缩写代码如下:

package com.stsi.pss.domain.Model;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.stsi.pss.domain.BaseClasses.AuditedEntity;
import com.stsi.pss.domain.Model.LookupEntities.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "Personnel", schema = "dbo", catalog = "PSSV2Db")
@EntityListeners(AuditingEntityListener.class)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope = PersonnelEntity.class)

public class PersonnelEntity extends AuditedEntity<String> {

    private Long id;
    private Long workforceId;
    private String workPhoneNumber;
    private String workEmailAddress;
    private String UPN;
    private String currentPosition;
    private Boolean isSponsored;
    private LocalDate dateOfSponsorship;
    private String sponsorPersonnelId;
    private Boolean isLEO;
    private Boolean isFERO;
    private Boolean requireComputerAndEmailAccess;
    private Boolean isPriorityCase;

    @Id
    @Column(name = "PersonnelId", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return id;
    }

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

....other getters/setters/columns/relationships definitions here....

}

当我执行搜索并且结果有2个PersonnelEntity对象时,第一个实体对象使用完整对象图表返回正常,但第二个PersonnelEntity对象只返回它的id(id:2)。响应类似于以下内容:

{
    "personnel": [
        {
            "id": 3,
            "creatorUserId": "sunil",
            "creationTime": "2018-06-03T03:09:02.963+0000",
            "lastModifierUserId": "sunil",
            "lastModificationTime": "2018-06-03T03:09:02.963+0000",
            "workforceId": 999323456,
            "workPhoneNumber": null,
            "workEmailAddress": null,
            "currentPosition": null,
            "isSponsored": null,
            "dateOfSponsorship": null,
            "sponsorPersonnelId": null,
            "isLEO": true,
            "isFERO": false,
            "requireComputerAndEmailAccess": true,
            "isPriorityCase": false,
            "building": {
                "id": 9,
                "creatorUserId": null,
                "creationTime": null,
                "lastModifierUserId": null,
                "lastModificationTime": null,
                "isActive": true,
                "buildingName": "Federal Building, Post Office and Courthouse",
                "buildingNumber": "275",
                "displayGlobal": false,
                "facilityName": "Federal Building, Post Office and Courthouse",
                "facilityNumber": "15",
                "personnel": [
                    3
                ],
                "locationCityState": {
                    "id": 95,
                    "creatorUserId": null,
                    "creationTime": null,
                    "lastModifierUserId": null,
                    "lastModificationTime": null,
                    "cityName": "WINCHESTER",
                    "isActive": true,
                    "personnel": [
                        3
                    ],
                    "building": [
                        9
                    ],
                    "stateCode": {
                        "id": 52,
                        "creatorUserId": null,
                        "creationTime": null,
                        "lastModifierUserId": null,
                        "lastModificationTime": null,
                        "code": "TN",
                        "description": "TENNESSEE",
                        "isActive": true,
                        "locationCityState": [
                            95,
                            {
                                "id": 93,
                                "creatorUserId": null,
                                "creationTime": null,
                                "lastModifierUserId": null,
                                "lastModificationTime": null,
                                "cityName": "NASHVILLE",
                                "isActive": true,
                                "personnel": [],
                                "building": [
                                    {
                                        "id": 80,
                                        "creatorUserId": null,
                                        "creationTime": null,
                                        "lastModifierUserId": null,
                                        "lastModificationTime": null,
                                        "isActive": true,
                                        "buildingName": "Nashville Resident Office",
                                        "buildingNumber": "342",
                                        "displayGlobal": false,
                                        "facilityName": "Nashville Resident Office",
                                        "facilityNumber": "19",
                                        "personnel": [],
                                        "locationCityState": 93
                                    },
                                    {
                                        "id": 81,
                                        "creatorUserId": null,
                                        "creationTime": null,
                                        "lastModifierUserId": null,
                                        "lastModificationTime": null,
                                        "isActive": true,
                                        "buildingName": "Estes Kefauver Federal Building Parking",
                                        "buildingNumber": "281",
                                        "displayGlobal": false,
                                        "facilityName": "Estes Kefauver Federal Building Parking",
                                        "facilityNumber": "256",
                                        "personnel": [],
                                        "locationCityState": 93
                                    }
                                ],
                                "stateCode": 52
                            },
                            {
                                "id": 94,
                                "creatorUserId": null,
                                "creationTime": null,
                                "lastModifierUserId": null,
                                "lastModificationTime": null,
                                "cityName": "KNOXVILLE",
                                "isActive": true,
                                "personnel": [],
                                "building": [
                                    {
                                        "id": 10,
                                        "creatorUserId": null,
                                        "creationTime": null,
                                        "lastModifierUserId": null,
                                        "lastModificationTime": null,
                                        "isActive": true,
                                        "buildingName": "Knoxville Resident Office",
                                        "buildingNumber": "283",
                                        "displayGlobal": false,
                                        "facilityName": "Knoxville Resident Office",
                                        "facilityNumber": "74",
                                        "personnel": [],
                                        "locationCityState": 94
                                    }
                                ],
                                "stateCode": 52
                            },
                            {
                                "id": 92,
                                "creatorUserId": null,
                                "creationTime": null,
                                "lastModifierUserId": null,
                                "lastModificationTime": null,
                                "cityName": "JACKSON",
                                "isActive": true,
                                "personnel": [],
                                "building": [
                                    {
                                        "id": 98,
                                        "creatorUserId": null,
                                        "creationTime": null,
                                        "lastModifierUserId": null,
                                        "lastModificationTime": null,
                                        "isActive": true,
                                        "buildingName": "Ed Jones Federal Building & Courthouse",
                                        "buildingNumber": "341",
                                        "displayGlobal": false,
                                        "facilityName": "Ed Jones Federal Building & Courthouse",
                                        "facilityNumber": "128",
                                        "personnel": [],
                                        "locationCityState": 92
                                    }
                                ],
                                "stateCode": 52
                            }
                        ]
                    }
                }
            },
            "locationCityState": 95,
            "personnelStatus": {
                "id": 1,
                "creatorUserId": null,
                "creationTime": null,
                "lastModifierUserId": null,
                "lastModificationTime": null,
                "status": "Active",
                "description": "Active",
                "isActive": true,
                "personnel": [
                    {
                        "id": 2,
                        "creatorUserId": null,
                        "creationTime": null,
                        "lastModifierUserId": null,
                        "lastModificationTime": null,
                        "workforceId": 9994323221,
                        "workPhoneNumber": "7034563452",
                        "workEmailAddress": "skosuri@epa.gov",
                        "currentPosition": "Developer",
                        "isSponsored": false,
                        "dateOfSponsorship": null,
                        "sponsorPersonnelId": null,
                        "isLEO": true,
                        "isFERO": false,
                        "requireComputerAndEmailAccess": true,
                        "isPriorityCase": false,
                        "building": {
                            "id": 1,
                            "creatorUserId": null,
                            "creationTime": null,
                            "lastModifierUserId": null,
                            "lastModificationTime": null,
                            "isActive": true,
                            "buildingName": "Robert C Byrd Courthouse and FOB",
                            "buildingNumber": "406",
                            "displayGlobal": false,
                            "facilityName": "Robert C Byrd Courthouse",
                            "facilityNumber": "107",
                            "personnel": [
                                2
                            ],
                            "locationCityState": {
                                "id": 109,
                                "creatorUserId": null,
                                "creationTime": null,
                                "lastModifierUserId": null,
                                "lastModificationTime": null,
                                "cityName": "CHARLESTON",
                                "isActive": true,
                                "personnel": [
                                    2
                                ],
                                "building": [
                                    1
                                ],
                                "stateCode": {
                                    "id": 58,
                                    "creatorUserId": null,
                                    "creationTime": null,
                                    "lastModifierUserId": null,
                                    "lastModificationTime": null,
                                    "code": "WV",
                                    "description": "WEST VIRGINIA",
                                    "isActive": true,
                                    "locationCityState": [
                                        {
                                            "id": 108,
                                            "creatorUserId": null,
                                            "creationTime": null,
                                            "lastModifierUserId": null,
                                            "lastModificationTime": null,
                                            "cityName": "WHEELING",
                                            "isActive": true,
                                            "personnel": [],
                                            "building": [
                                                {
                                                    "id": 48,
                                                    "creatorUserId": null,
                                                    "creationTime": null,
                                                    "lastModifierUserId": null,
                                                    "lastModificationTime": null,
                                                    "isActive": true,
                                                    "buildingName": "The Valley Building",
                                                    "buildingNumber": "150",
                                                    "displayGlobal": false,
                                                    "facilityName": "The Valley Building",
                                                    "facilityNumber": "121",
                                                    "personnel": [],
                                                    "locationCityState": 108
                                                },
                                                {
                                                    "id": 49,
                                                    "creatorUserId": null,
                                                    "creationTime": null,
                                                    "lastModifierUserId": null,
                                                    "lastModificationTime": null,
                                                    "isActive": true,
                                                    "buildingName": "Wheeling Field Office",
                                                    "buildingNumber": "152",
                                                    "displayGlobal": false,
                                                    "facilityName": "Wheeling Field Office",
                                                    "facilityNumber": "137",
                                                    "personnel": [],
                                                    "locationCityState": 108
                                                }
                                            ],
                                            "stateCode": 58
                                        },
                                        109
                                    ]
                                }
                            }
                        },
                        "locationCityState": 109,
                        "personnelStatus": 1,
                        "personnelType": {
                            "id": 1,
                            "creatorUserId": null,
                            "creationTime": null,
                            "lastModifierUserId": null,
                            "lastModificationTime": null,
                            "description": "Federal Employee",
                            "isActive": true,
                            "personnel": [
                                2
                            ]
                        },
                        "programOffice": {
                            "id": 1,
                            "creatorUserId": null,
                            "creationTime": null,
                            "lastModifierUserId": null,
                            "lastModificationTime": null,
                            "officeName": "OFFICE OF ENVIRONMENTAL INFORMATION",
                            "officeCode": "28000000",
                            "abbreviation": "OEI",
                            "isActive": true,
                            "personnel": [
                                2
                            ]
                        },
                        "personnelAliases": [],
                        "personnelDetail": {
                            "id": 1,
                            "creatorUserId": null,
                            "creationTime": null,
                            "lastModifierUserId": null,
                            "lastModificationTime": null,
                            "firstName": "Sunny",
                            "middleName": null,
                            "lastName": "Kosuri",
                            "suffix": null,
                            "foreignIdNumber": null,
                            "personnelAliasId": null,
                            "dateOfBirth": "2012-08-04T04:00:00.000+0000",
                            "cityOfBirth": "Hyderabad",
                            "isUSCitizen": true,
                            "address1": "11683 chanceford drive",
                            "address2": null,
                            "addressCity": "Woodbridge",
                            "postalCode": "22192",
                            "homePhoneNumber": "7033495634",
                            "cellPhoneNumber": "2017665523",
                            "homeEmailAddress": "sunilkosuri@gmail.com",
                            "height": "6.0",
                            "weight": "175",
                            "hairColor": "Black",
                            "eyeColor": "Blue",
                            "birthStateCode": null,
                            "birthCountryCode": null,
                            "citizenshipCountryCode": null,
                            "addressStateCode": null,
                            "addressCountryCode": null,
                            "personnel": 2,
                            "ssn": "175445565"
                        },
                        "personResponsible": null,
                        "pivcardInfo": [],
                        "upn": "042100005264",
                        "upnflagCode": {
                            "id": 1,
                            "creatorUserId": null,
                            "creationTime": null,
                            "lastModifierUserId": null,
                            "lastModificationTime": null,
                            "code": "SYSTEM",
                            "description": "SYSTEM generated code",
                            "isActive": true,
                            "personnel": [
                                2
                            ]
                        },
                        "vcases": []
                    },
                    3
                ]
            },
            "personnelType": null,
            "programOffice": {
                "id": 8,
                "creatorUserId": null,
                "creationTime": null,
                "lastModifierUserId": null,
                "lastModificationTime": null,
                "officeName": "OFFICE OF ADMINISTRATION AND RESOURCES MANAGEMENT",
                "officeCode": "30000000",
                "abbreviation": "OARM",
                "isActive": true,
                "personnel": [
                    3
                ]
            },
            "personnelAliases": [],
            "personnelDetail": {
                "id": 7,
                "creatorUserId": "sunil",
                "creationTime": "2018-06-03T03:09:02.802+0000",
                "lastModifierUserId": "sunil",
                "lastModificationTime": "2018-06-03T03:09:02.802+0000",
                "firstName": "Sunil",
                "middleName": null,
                "lastName": "Kosuri",
                "suffix": null,
                "foreignIdNumber": null,
                "personnelAliasId": null,
                "dateOfBirth": "1972-02-02T04:09:02.695+0000",
                "cityOfBirth": "Arlington",
                "isUSCitizen": true,
                "address1": "11632 Chanceford Drive",
                "address2": null,
                "addressCity": "Fairfax",
                "postalCode": "22192",
                "homePhoneNumber": "7033495634",
                "cellPhoneNumber": null,
                "homeEmailAddress": "sunilkosuri@gmail.com",
                "height": "6.0",
                "weight": "175",
                "hairColor": "Black",
                "eyeColor": "Brown",
                "birthStateCode": null,
                "birthCountryCode": null,
                "citizenshipCountryCode": null,
                "addressStateCode": null,
                "addressCountryCode": null,
                "personnel": 3,
                "ssn": "123432222"
            },
            "personResponsible": null,
            "pivcardInfo": [],
            "upn": null,
            "upnflagCode": null,
            "vcases": []
        },
        2
    ]
}

我也配置了以下内容:

package com.stsi.pss;

import com.stsi.pss.application.Email.Dto.SendEmailInput;
import com.stsi.pss.domain.Authorization.permissions.PermissionsEntity;
import com.stsi.pss.domain.Authorization.roles.RolesEntity;
import com.stsi.pss.domain.Authorization.users.UsersEntity;
import com.stsi.pss.domain.Email.MailMessagesEntity;
import com.stsi.pss.domain.Model.*;
import com.stsi.pss.domain.Model.LookupEntities.*;
import com.stsi.pss.domain.Organizations.OrganizationUnitsEntity;
import com.stsi.pss.domain.Settings.SettingsEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;

import javax.persistence.EntityManager;

@Configuration
public class RepositoryConfig extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(PersonnelEntity.class);

}

我在这里做错了什么?我应该更改什么才能返回对象图的搜索结果INSTEAD中的实体的完整对象图仅针对第一个实体和其余的ID?

0 个答案:

没有答案