microservices-practical / microservices-v5

Learn Microservices with Spring Boot - v5
20 stars 28 forks source link

Add empty constructor #1

Closed dbgod closed 6 years ago

dbgod commented 6 years ago

Add empty constructor so gamification can successfully process messages from social-multipliation.

Without the empty constuctor the following error is thrown whenever gamification attempted to handle an incoming message: Could not read JSON: Can not construct instance of microservices.book.gamification.event.MultiplicationSolvedEvent: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

mechero commented 6 years ago

Hi @dbgod, thanks for the detailed description. JPA is not used for the event, so the empty constructor is not required. JSON serialization uses the required-arguments constructor built by Lombok in this case to create the object. It must be something else what is causing that problem:

dbgod commented 6 years ago

Thanks for responding! I'm reallying enjoying working through your book!

I didn't consider that JDK version, versions of the dependencies, and configuration would have contributed to my troubles. I have Java 1.8.0_171 installed, There are two deltas in my pom: later versions of Spring Boot (1.5.10.RELEASE) and Project Lombok (1.16.20). Lastly, in my application.properties the path to my H2 database file is "C:/Users/Chris/gamification" -- "~/gamficiation" wouldn't work for me, presumably because I'm running in Windows and not Linux.

mechero commented 6 years ago

OK, I got it, it's Lombok. Even though it's a minor version update, this new dependency brings some updates to make everything works with Java 9, which break the previous behavior of Lombok. It's nicely explained by several users in this issue: what happens is that Lombok is no longer using the parameter names and therefore Jackson can't find a proper constructor.

Your solution is valid. Another possibility to avoid creating boilerplate code is, as that issue suggests, creating a file called lombok.config in the root of the gamification project and adding the line

lombok.anyConstructor.addConstructorProperties=true

I just tried this and it works. If you're using your IDE to run the apps, remember to rebuild the source code after adding the config file.

I won't merge the pull-request since some readers might be confused by the code mismatch but I'll definitely add a section to the README files.

Good catch! I'm glad you're enjoying the book 😄

dbgod commented 6 years ago

Yes, I see: with lombok.config, Lombok adds the annotation "@java.beans.ConstructorProperties({"multiplicationResultAttemptId", "userId", "correct"})" to the cosntructor for MultiplicationSolvedEvent. Thanks again... onto Chapter 5!

rahiakela commented 6 years ago

After doing the above solution, I am getting this error: error

Please suggest any solution because I am struggling for hours.

mechero commented 6 years ago

@rahiakela, could you provide more details? Are you also using a new version of Lombok? Did you try creating the empty constructor or using the Lombok configuration file?