I am writing a java-library that will be included included as a dependency in other projects. I want some of the library's classes to de/serializable with Jackson's ObjectMapper. Out of habit, I have used annotations to tell Jackson how to de/serialize my classes.
Will the annotations' functionality get exported by the library (like api)?
Will a dependent application be able to use the an ObjectMapper created by the default constructor? Is any further configuration necessary?
Can I put the annotations into a custom Jackson Module for dependent applications to register with their ObjectMapper?
I have been very surprised to find a dearth of documentation for this use case.
Related
I am using the openapi-generator-maven-plugin to generate model sources.
Is there a way to generate them only with fields and without any access methods?
I want the access methods to be generated via lombok with the additionalModelTypeAnnotations configOption in the maven configuration of the openapi-generator-maven-plugin
You can implement those customizations by changing the Mustache templates
Fetch the templates of the Java framework (ie spring-boot) you want to use (ie openapi-generator-cli author template -g spring --additional-properties=library=spring-boot -o tmp/mytemplates
Modify the local model.mustache file to import the packages you want (lombok) and pojo.mustache to remove the getters/setters. There might other customisations necessary (each framework templates are different) but this the recommended approach.
As #beppe suggested in their answer, this can easily be done by modifying the mustache template. However, I highly advice against it. In doing so, you discard many of the the features included with the model objects created by the generator. Some of the features you lose are:
Bean Validation
Jackson Serialization Library Support
Support for vendor extensions
By adding the Lombok annotation to the generated files, you are basically ignoring the main point of the generator, which is to generate files that support each other. You are instead using a generator to call a generator.
Finally, the amount of customization necessary to make the lombok library work with the mustache files is much more than is really worth it. Sure, you can add #Data or #Jacksonized to your model, but what about #JsonIgnore? Do you want to use parameters in your Lombok calls? Because you can't set the builder classname via #Builder(builderClassName = "EmployeeBuilder"). That would be hardcoded. Instead, you'd have to use mustache parameters, such as #Builder(builderClassName = {{$builderClassName}}), which you'd then have to define elsewhere. What about #Builder vs #SuperBuilder? Which one do you use in which situation? How do you define and template it?
At this point, you might as well rewrite the entire pojo.mustache to be able to use the lombok annotations. But, what if you need the old pojo.mustache functionality elsewhere? Now you need to write a custom generator to determine which mustache to use in which situations.
It is best to just build the domain models as the openapi-generator intends and use them as they are. You can add annotations if you need to via vendor extensions such as x-field-extra-annotation and x-class-extra-annotation if you really feel the need to.
I am using a dependency JAR in my project which has a custom annotation "#monitor"
This annotation is being used at a lot of places in the code.
Going forward, I would like to perform a quick test if there are any performance improvements if we ignore this annotation(As there are reflection api calls due to this).
I do not want to remove annotation classes from the dependency JAR as other projects might be using this. I also do not want to go to every method in my code or inside JAR to remove the "#monitor" annotation from methods.
Is there a way in SpringBoot2 to ignore a particular annotation from a spring configuration class?
Is there a way in SpringBoot2 to ignore a particular annotation.
I am developing a plugin for Intellij IDEA 2018.2+ which will provide some additional inspections.
I have already learnt that there is a plugin.xml file which is the "heart" of plugin and is responsible for main behaviour of plugin.
As I understand, to implement (for example) additional inspection behaviour we need to define inspectionToolProvider in plugin.xml and inherit InspectionToolProvider interface. The same structure is defined for other extensions - we need to define something in .xml and implement some interface.
What bothers me is that if I want to implement some more-or-less complex algorithm, it looks like I need to use lots of static methods and utility classes, because I haven't found a way to use DI (e.g. Spring one) during plugin development.
Some examples in Intellij IDEA SDK docs also show "helper" methods as static ones defined in utility classes.
So overall question: is there a way to use dependency injection during Intellij IDEA plugin development?
IntelliJ IDEA has its own dependency injection, managed by PicoContainer. It allows you to inject any component or service into the constructor of any component, service or extension created on the same or lower level (possible levels are application, project and module). To use it, you simply declare a constructor parameter of the corresponding type; you do not need to apply any extra annotations.
You can also start your own DI container (using Spring or any other framework) in your plugin, but then it will be your own responsibility to support the injection of core IntelliJ IDEA components.
I'd like to define some commonly used or generic service classes that should be used/shared by different projects. These common services should already make use of #Transactional, #Autowired and other Spring related stuff. So, I somehow have to define a spring context for these services to work.
Is it possible to put these services in a single external jar library that can then be used/imported by other (child)-projects? How could I create such a "personal framework"?
What you could do is create a maven (or gradle) module that contains the code you desire to be reusable and also have a spring configuration (either XML or Java Config) that will be imported by the project that uses the module (either with or having component scanning pick up the #Configuration class of the module).
This is the opposite of what most Spring users are clamoring to do, but I was wondering if its possible to specify that Jackson 1.9.7 be used by Spring (3.1.2) and NOT Jackson 2+.
The project I'm working on relies on other projects that use Jackson 2, so it get pulls in as a transitive dependency. From there Spring picks it up to handle JSON serialization. This in turn borks my application because of custom annotations I've created that require the Jackson version be 1.9.7.
Not sure if this helps, but note that you can also add both Jackson 1 and Jackson 2 annotations in value classes. I have done this at work, to help transition from Jackson 1.9 to Jackson 2.x. And in fact different parts of code use different version: unit test helper methods were migrated first, and later on production code, section by section.
As with library version, use of two sets of annotations is not optimal, but doing so may help reduce risk of version upgrade.
Finally, it is also possible to use an AnnotationIntrospector that can use both sets of annotations (I don't have a link at hand, but I know a Jackson user published version he created); usually Jackson 2 JacksonAnnotationIntrospector that also recognizes Jackson 1 annotations. This avoids duplication of annotations and makes it possible to upgrade code first, then convert annotations.