Saturday, 27 July 2024

jakarta.json.bind.JsonbException: Cannot create instance of a record: class SomeClass, Multiple constructors found.

So I've been trying to use Java Records as data holders for JSONB serialisation and deserialisation. And it works well, until I encountered the following:

jakarta.json.bind.JsonbException: Cannot create instance of a record: class SomeClass, Multiple constructors found.

It turns out, I can put in an annotation @JsonbCreator on it, as explained in reference [1].

It just takes a bit of work, as I cannot put the annotation on top of a record class, it needs to be put on a constructor. The way this works, is to create a compact constructor in the record.

The compact constructor is usually used to put in some sort of validation in the record class upon instantiation, but here we need it for the annotation @JsonbCreate.

Brian Goetz in the comments of reference [2] indicates that this is the proper way to do it.

Unfortunately, it causes one of those "empty constructors" messages of SonarLint.

Warning:(31, 10) Remove this redundant constructor which is the same as a default one.

I assume SonarLint will fix this eventually.

For completeness, my code:

public record AdminItem(Integer id,
                        String belongsto,
                        Long room,
                        String shopkeeper,
                        String owner,
                        LocalDateTime creation)
{

  @JsonbCreator
  public AdminItem
  {
    // empty constructor, because I need to put the annotation @JsonbCreator somewhere.
  }

  public AdminItem(Item item)
  {
    this(item.getId(), 
        item.getBelongsTo() == null ? null : item.getBelongsTo().getName(),
        item.getRoom() == null ? null : item.getRoom().getId(),
        null,
        item.getOwner() == null ? null : item.getOwner().getName(),
        item.getCreation());
  }

}

References

[1] Carlos Chacin - 💾 Java 14 Records 🐞 with JakartaEE JSON-B
https://carloschac.in/2020/04/20/java-records-jsonb/
[2] StackOverflow - Constructor annotation on java records
https://stackoverflow.com/questions/67168624/constructor-annotation-on-java-records

Thursday, 18 July 2024

Angular and Updates

Recently was interested if and when Angular upgrades to a new release, as I found myself skipping release 17, and the update process can be a tiny bit troublesome if you skip a major version.

Angular has Semantic Versioning, and releases a new major version every 6 months. Angular 19 will possibly be out and about end of November 2024.

Luckily, from the references below, there's some things that I can hold on to:

  • a major version may require a bit of work by the developer.
  • a minor version can be installed without problems immediately. No developer assistance is expected during update. However, you can optionally decide to use new features in your code or change old features to use new stuff.
  • a fix/patch version can be installed without problems immediately. No developer assistance is expected during update.

And GitHub tends to complain (a lot!) if my Angular projects are a tiny bit old, and there're Security concerns with the javascript dependencies Angular uses.

With that in mind, the following quote on the website seems very good to know:

"We only make npm dependency updates that require changes to your applications in a major release. In minor releases, we update peer dependencies by expanding the supported versions, but we do not require projects to update these dependencies until a future major version. This means that during minor Angular releases, npm dependency updates within Angular applications and libraries are optional."

References

Angular - Roadmap
https://angular.dev/roadmap
Angular - Releases
https://angular.dev/reference/releases
GitHub - Angular Public API Surface
https://github.com/angular/angular/blob/main/contributing-docs/public-api-surface.md
Angular Blog
https://blog.angular.dev/
Angular Dev
https://angular.dev/