Skip to content
Advertisement

Spring @PostMapping can’t create entity from provided JSON

I am learning springboot and I’ve encountered problem while creating new entities through POST requests. While creating one entity I can use Json in POST request but while creating others I can’t.

Method working as expected

The one working properly looks like this.

JavaScript

I have also created data transfer object for this entity:

JavaScript

and data transfer object mapper:

JavaScript

controller for creating this entity looks like this:

JavaScript

and GroupService looks like this:

JavaScript

Swagger-ui console for this method looks like this:swagger-console-group

and the response looks like expected:swagger-response-group

Malfunctioning method

Then I have another entity called Participant that is connected with AccountingGroup through a relation:

JavaScript

it also has it’s data transfer object:

JavaScript
JavaScript

and controller:

JavaScript
JavaScript

So logic here is similar to this in AccountingGroup. However here I can’t send Json object in the POST request. Swagger-ui console looks like this:swagger-console-participant and the response looks like this:swagger-response-participant So participant is added correctly however I can’t use Json in the requet and I don’t know why. When I tried to make a POST request with json through Python it worked fine for AccountingGroup but for Particiapnt I got this error:

JavaScript
JavaScript

Advertisement

Answer

I finally solved this problem. In GroupsController I had the following import

JavaScript

and in ParticipantsController

JavaScript

When I set both imports to this one from GroupsController everything works as expected.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement