ETLJ
ETLJ

Reputation: 301

Spring Framework Responses from POST

What is the standard object design for accepting a POST request from a client, saving the record to the database, and then returning a response back to the client? I'm working with the Spring framework.

Should I be sending back the entity and hiding properties that aren't necessary for the response?

@RestController
public class SomeController {

    private final SomeService service;

    @PostMapping(value = "/post/new", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<SomeEntity> post(@RequestBody final SomeEntity someEntity) {
        SomeEntity savedEntity = service.save(someEntity);
        return ResponseEntity.ok(savedEntity);
    }
}

@Entity
@Table(name = "posts")
public class SomeEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "title")
    private String title;

    @Column(name = "body")
    @JsonIgnore
    private String body;

    @JsonIgnore
    @Column(name = "deleted_ind")
    private boolean deleted;

    @JsonIgnore
    @Column(name = "author")
    private String author;

    @Column(name = "created_at")
    private LocalDateTime createdAt;

}

or would I accept some sort of POST request object that I convert to an entity, then re-assemble the entity into a response?

@JsonIgnoreProperties(ignoreUnknown = true)
public class SomePostRequestResource {

    private String title;

    private String body;

    private String createdAt;

}

@RequiredArgsConstructor
@RestController
public class SomeController {

    private final SomeService service;
    private final SomeResourceAssembler resourceAssembler;

    @PostMapping(value = "/post/new", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<SomePostRequestResource> post(
        @RequestBody final SomePostRequestResource someResource
    ) {
        SomeEntity savedEntity = service.convertToEntityAndSave(someResource);
        SomePostRequestResource response = resourceAssembler.toResource(savedEntity);
        return ResponseEntity.ok(response);
    }
}

But then maybe I only want to send back the createdAt, would I hide the other properties in the SomePostRequestResource, or do I need another object to represent the response, which only has the property I want to send back?

I would also appreciate any book or article suggestions related to desigining objects for use with a RESTful API. I have seen articles concerning how to design and name the endpoints, but not so many concerning how to design the objects on the backend.

Upvotes: 0

Views: 102

Answers (1)

Ehab Qadah
Ehab Qadah

Reputation: 570

I would recommend you create a DTO class for the incoming/outgoing data containing the filed that are set/viewable by the client like:

public class SomeEntityIncomingDto {


    private String title;

    ....


}
public class SomeEntityOutgoingDto {


    private Long id;

    private String title;

    ....


}

On the other hand, You won't need to map your persistence entities to DTOs and vice versa manually, you can use a library like ModelMapper or MapStruct that handles the conversion automatically.

Upvotes: 1

Related Questions