Stomp Authentication

In a previous blog post, I shared my first experience with Stomp and how easily it was to use with StompJS and Spring Boot.
There are however a couple of caveats you have to keep in mind regarding authentication. That is what I want to explore in more depth in this blog post. I will share a lot of code and explain it as well as I can.

Continue reading

First WebSocket & Stomp Experience

For a project where I want to be able to offer more interactivity and concurrent modifications, I decided to use a WebSocket. Since my backend is written in Spring, after some research, I decided to use Stomp on top of the WebSocket layer as that would give me the best integration with Spring. All in all, the experience went pretty smooth, but there were some caveats which I will reveal in this blog post.

Continue reading

Multiple Inheritance Actions

In a previous blog post, I introduced my new approach regarding updating objects. Instead of just performing the updates in the service, I approached it in a true object oriented approach and made actual objects for the actions. In a new project I was using this approach as well, but I encountered a problem regarding my inheritance tree for these actions. In this blog post I will go into detail what my problem was and how I solved it.

Continue reading

Clean Method Signature Patterns

Naming a method properly is crucial in making your code readable and maintainable, it is however also very difficult and comes with lots of caveats and dilemmas. A method name is however not the only part of the full signature, variables are included as well and thus can also play a role in distinguishing methods. In this blog post, I will explore some common bad practices and see what we can do to fix it.

Continue reading

Global State SecurityContextHolder

Everyone agrees with the fact that having some global state is a bad idea and should be avoided. The cases in which this is really the only option are very limited. One case however, is in Spring where we get the SecurityContextHolder object. From the point of view of Spring, being a framework, this is the only way to make this information accessible to the parts of your application where you need it. This however does not mean you should rely on it everywhere in your application. In this blog post, we will explore why this is a bad idea, and what you should be doing instead.

Continue reading

API Error Messages

When designing an API, we tend to only focus on successful requests. This often leads to error responses that are not useful and often don’t contain any information. I am guilty of this myself, and in my own projects I typically only use the HTTP status code and do not include a body with more information. In this blog post, I want to explore what a good API error message should look like and which considerations we have to take into account.

Continue reading

Moving Responsibilities Into The BDO

As I have mentioned in some earlier blog posts, the usage of Spring puts a lot of focus of having your logic in services. While this makes sense for some things, it comes with the big issue that your objects don’t enforce anything and if you bypass your service you may cause inconsistencies. It may also mean that you have to duplicate parts of the logic in multiple services or extract it to a separate service. When working on my PATCH actions, It seemed like I found a way to incorporate checks into the BDO instead of having it in my service.

Continue reading

Testing A Service

When using Spring or a similar framework, which most Java Web applications use, the usage of dependency injection heavily leans into writing all of your logic in services instead of objects. When you embrace the service oriented approach, you may end up with situations where testing your logic can become difficult. In this blog post I want to expand a bit on the difficulties of testing services.

Continue reading

Testing Object Correctness Without Getters/Setters

When we are testing classes, functions or anything really, you need to verify the correct things have been done. In some cases, this may depend on some internal state, in others it can be that a certain object was created. In both cases you will want to check that the data is correct, but how do you check this? An easy solution is to have getters or other methods to inspect that internal state. This leads to the question, whether an object should always provide getters to make this possible, or is there another way?

Continue reading

Mocking Mappers?

In the past I never would mock a mapper, mostly because this typically are static methods and mocking static methods either wasn’t easily possible or (as mentioned in the previous blog posts) result in less clean test code. With the usage of BDOs there is a lot more mapping going on, and I have been reconsidering whether or not mocking mappers is a good idea.

Continue reading

Mockito Advanced Mocking

When I started as a junior developer, the typical setup was to use Mockito in combination with PowerMock. This was required because Mockito couldn’t perform more advanced mocking such as constructors, static fields or final classes. Things have changed now and since some time, Mockito now offers the ability to mock constructors and static methods. In this blog post I will go into more detail on how you can use these features.

Continue reading

Clean Services

In the past my services would always take DTOs as arguments, convert them to DAOs and pass those to my repositories. This limits the re-usability of your service, as every caller now has to convert their object into that DTO type, or your services needs to support duplicate methods for all different DTO types. Ever since, I have been trying to get rid of this, and push all this boilerplate to the ‘outer’ layers.

Continue reading

Adding A Repository Facade

In the evolution of BDO’s, especially with my patch actions, It seemed very nice to move more logic out of the service. Such that the service fully works on BDOs and isn’t aware of any DTO or DAO, more like the onion architecture. This is not a must for me, but just as an experiment, I do see some value in it. In this blog post, I attempt to make the service work without knowledge of the DAO, in the next blog post, we will put everything together and make the service work only on BDOs.

Continue reading

REST API & Actions

A REST API is built upon describing resources where the URL specifies the resource and the HTTP action specifies the operation, but sometimes you just need more than create, update and delete. So what do you do in this case? In this blog post, I want to discuss a couple of possibilities.

Continue reading

How To Handle Views

In some previous blog posts, I already expressed my empathy towards views and that I use them quite often. I like views, as they are a way to extract information from a database outside of your regular entities. Where if you would want combined information from separate entities, you could fetch them separately and join them in Java, doing it with a view allows you to do all the processing in your database, which saves you some queries and processing in Java. With the separation of DTOs, BDOs and DAOs however, how should you handle views?

Continue reading

Decoupling BDOs

This is a controversial action, even for me, but with my recent exploration of BDOs I am starting to wonder whether it makes sense to have coupled BDOs (in the sense that one object has a reference to a linked object) or whether having them loosely coupled and just have the ids on the BDO’s is better.

Continue reading