Как JSON вернуть детали класса дочерней модели, используя JPA и Springboot - PullRequest
0 голосов
/ 04 февраля 2020

Я новичок в весенней загрузке. У меня есть два класса моделей как Party и PartyCategory, и у меня есть отношение @ManytoOne.

В запросе на вечеринку JSON я хочу передать только идентификатор PartyCategory и ожидаю получить все детали, которые передаются в PartyCategoryResponse DTO. (Я получаю нулевые значения на данный момент при передаче идентификатора)

Первичный ключ - это идентификатор для класса модели Party, который является внешним ключом для класса модели PartyCategory.

JSON Запрос участника:

{
        "name":"Party A",
        "description":"Very Good ..!!!",
        "category":{
            "ID":32    (here passing ID as 32)
        },
        "image":{
            "ID":2
        },
        "addresses":[
            {
                "name":"jhhhkhk",
                "location":{
                    "x":"20",
                    "y":"30"
                },
                "contacts":[{
                    "type":{
                        "ID":1
                    },
                    "value":"gjhgj"
                },
                {
                    "type":{
                        "ID":2
                    },
                    "value":"jhkjkgj"
                }]
            }

            ]
    }

JSON Ответ партии:

{
    "name": "Party A",
    "description": "Very Good ..!!!",
    "category": {
        "labelEn": null,     (This value should not be null, should be fetched from PartyCategory table)
        "labelAr": null,     (This value should not be null, should be fetched from PartyCategory table)
        "id": 11
    },
    "image": {
        "name": null,
        "type": null,
        "path": null,
        "id": 12
    },
    "addresses": [
        {
            "name": "jhhhkhk",
            "location": {
                "x": "20",
                "y": "30"
            },
            "contacts": [
                {
                    "type": {
                        "labelEn": null,
                        "labelAr": null,
                        "id": 16
                    },
                    "value": "gjhgj"
                },
                {
                    "type": {
                        "labelEn": null,
                        "labelAr": null,
                        "id": 18
                    },
                    "value": "jhkjkgj"
                }
            ]
        }
    ]
}

PartyController. java: -

  @PostMapping(value = "/party/save")
    public ResponseEntity saveParty(@RequestBody PartyRequest partyRequest) {

            PartyResponse partyResponse = partyService.saveParty(partyRequest);
            return ResponseEntity.ok(partyResponse);

    }

PartyRequest. java : -

public class PartyRequest {

    private Integer ID;
    private String name;
    private  String description;
    private PartyCategory category;
    private File image;
    private List<Address> addresses;

..setters and getters...
}

PartyResponse. java: -

public class PartyResponse {


    private String name;
    private String description;
    private PartyCategoryResponse category;
    private FileResponse image;
    private List<AddressResponse> addresses;
..setters and getters...
}

PartyServiceImpl. java: -

@Service
public class PartyServiceImpl implements PartyService {

    @Autowired
    PartyRepository partyRepository;
    ModelMapper modelMapper = new ModelMapper();

    @Override
    public PartyResponse saveParty(PartyRequest partyRequest) {
        if (partyRequest.getName() == null) {
            throw new RuntimeException("Parameter name not found in request");
        } else if (partyRequest.getDescription() == null) {
            throw new RuntimeException("Parameter description not found in request");
        } else if (partyRequest.getAddresses() == null) {
            throw new RuntimeException("Parameter address not found in request");
        } else if (partyRequest.getCategory() == null) {
            throw new RuntimeException("Parameter Partycategory not found in request");
        }
        Party savedParty = null;
        if (partyRequest.getID() != null) {
            Party oldParty = partyRepository.findFirstValidById(partyRequest.getID());
            if (oldParty != null) {
                oldParty.setName(partyRequest.getName());
                oldParty.setDescription(partyRequest.getDescription());
                oldParty.setAddresses(partyRequest.getAddresses());
                oldParty.setCategory(partyRequest.getCategory());
                savedParty = partyRepository.save(oldParty);
            } else {
                throw new RuntimeException("Cannot find group with identifier: " + partyRequest.getID());
            }

        } else {
            Party party = modelMapper.map(partyRequest, Party.class);
            savedParty = partyRepository.save(party);

        }
        PartyResponse partyResponse = modelMapper.map(savedParty, PartyResponse.class);
        return partyResponse;

PartyService. java: -

public interface PartyService {

    PartyResponse saveParty(PartyRequest partyRequest);

PartyCategoryRequest.java :-

public class PartyCategoryRequest {

    private PartyCategory category;

    public PartyCategory getCategory() {
        return category;
    }

    public void setCategory(PartyCategory category) {
        this.category = category;
    }
}

PartyCategoryResponse. java: -

public class PartyCategoryResponse {


    private Integer ID;
    private String labelEn;
    private String labelAr;

setters. and getters...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...