"Stateless architectures will never work" - said some SOAP lover 10 years ago |
I liked JAX-RS since 10 years ago I have to repeat code, like check nullable entities and return 404, create entities and build the URI, check if parent resources are found before getting the list of child resources and so on... The basic solution is distribute WebApplicationException throws, then create an exception mapper to create a suitable response when such exception is caught... That's not elegant nor easy to maintain.
Well, at this point, if you came here looking for a solution, well, I don't have it. I mean, I try different approaches, see all these bad ideas I had:
- This JEE app scan open data and expose them in a REST API and I had this ugly "utils" https://github.com/sjcdigital/repasse-server/blob/master/repasse-server/src/main/java/org/jugvale/transfgov/utils/JaxrsUtils.java
- This throws exceptions when object is null https://github.com/Jug-Vale/certificates/blob/master/certificate-generator/src/main/java/org/jugvale/certificate/generator/rest/ResourceUtils.java
The list would go on, but I want to quickly introduce the new approach I am working one, see this GIST:
The methods from RESTUtils classes allow us to verify a given object and then run some code that will verify a given entity and do other action to build the response. For example, the method checkEntityAndUpdate is useful when you are updating an object, but first you must verify if it is not null (more specifically because PanacheEntity.findById returns null), if it is null 404 is returned, otherwise the consumer propsUpdate can be used to update some of attached object, and this is very important. This is easy to read and helpful, but still we need to repeat some code. With Quarkus and Panache I started an abstract class which was supposed to transform entities operations into suitable HTTP responses, letting us focus on HTTP mapping to our resource methods, but I faced a bug and gave up for now.
The question on this post title remains: What is the final solution to reuse code in JAX-RS resources? What do you use to avoid repeating code? An utility class? A magical framework? Please let me know!
The question on this post title remains: What is the final solution to reuse code in JAX-RS resources? What do you use to avoid repeating code? An utility class? A magical framework? Please let me know!
Nenhum comentário:
Postar um comentário