DEV Community

Discussion on: From 'A' to 'Web App': Build an API in Java

Collapse
 
robole profile image
Rob OLeary

Thanks David. Could you point me to a code example of the wrapper class you mentioned?

Collapse
 
zonaut profile image
David Debuck • Edited

I just write my own according to the needs. An example is as follows.
If properties are null than they are not included (Include.NON_NULL) in the json serialization which makes it re-usable for optional properties also

@Getter
@Setter
@JsonInclude(Include.NON_NULL)
public class ResultListDto<T> {

    @JsonProperty("results")
    private List<T> results;

    @JsonProperty("paging")
    private ResultPagingDto paging;

    public ResultListDto() {
    }

    public ResultListDto(List<T> results) {
        this.results = results;
    }
}

Spring HATEOAS for example uses the following github.com/spring-projects/spring-...

Thread Thread
 
robole profile image
Rob OLeary

I see! I am familiar with the DTO pattern. For HATEOAS, its a good use case for a DTO, appending the navigation links without adding logic to the model keeps your classes focused. If I use Spring, I tend to use one of the Spring Data libraries, in your repositories, you can add pagination and sorting, which adds optional parameters to your endpoints. I haven't encountered other situations where I felt a DTO would help me tbh. I was planning to build a complete API soon to see, so you never know!

Thread Thread
 
zonaut profile image
David Debuck • Edited

It's not really a DTO pattern in how people where used to it.
I keep my models only for internal logic and use dto's when I want to expose something to the public. I don't use model mappers that convert a model to a dto, this is just a waste of time and resources. Even with Spring you can read a query directly into a dto and this way my objects are very lean and I can be very specific in what I need to query and expose.
The reason I have my own wrapper object is while I love HATEOAS they have a serious performance impact. For most properties they need to query the DB. I mostly want a count or a next or previous page. I also include other information other than paging so that's also another reason.
Creating dto's for incoming request is in my opinion always a good idea.
I love Spring but frameworks like Spring Boot sometimes take those extra steps that can cause huge problems down the road easily in big enterprise applications or even small ones for that matter, the first thing I always do is set spring.jpa.open-in-view to false, if you don't know what this does you can take a look at it on vladmihalcea.com/the-open-session-...
At the end of the day we need to do what feels most comfortable to us so it's great to see different views on these kinds of subjects.

Thread Thread
 
robole profile image
Rob OLeary • Edited

I understand the motivation. It can be tricky to optimize performance when you use a framework sometimes, you need to override or exclude certain features, and it may not be something that the framework has considered as a use case. Without taking steps like you suggested, the performance of an application would be good for a lot of users. It's a similar problem space that GraphQL looks to solve, request only what you want, and fetch nothing extra from the backend. I am interested in looking into GraphQL.

The challenge I was tackling in this article, and maybe in a follow-up article, is to give a clear and complete path into learning web apis and using spring to do that, what is out there is not very approachable. I was trying to introduce this subject to students who were beginning, and I couldnt assume that they knew about HTTP and design patterns, it is tough to get to a point where you can feel confident making web apps. There isn't many complete example applications out there, the Spring Pet Clinic is still there, but it is a bit out of date.

Thread Thread
 
zonaut profile image
David Debuck

Yes, you're completely right about this, it's not always straight forward for beginners on where to start with these kinds of things.
GraphQL is indeed the next best thing right now and with good reasons, even Github has switched their latest API to it. Bandwidth is very costly so it's always a good idea to save those bytes :p

There are a couple of good new frameworks lately and Micronaut is one of them. I even feel that it feels somewhat simpler for beginners? Maybe it's just me. Testing also seems somewhat more natural to me.

I'm still sticking to Spring though at the moment, I feel it's somewhat more complete at this point in time for my needs.

Anywho, thanks for the article, I look forward to the next one.