The following refers to the generation of the Spring boot server stubs with swagger codegen (-l spring).
Is there any option for Swagger codegen binary (current: v2.3.1) to generate the dto models without the hashCode() and equals() methods?
I did not found any options in the config-help. I want the generated dto models to extend an abstract class where the hashCode() and equals() methods are already declared and therefore shouldn't be overwritten in the generated model classes.
One way is to customize the mustache templates (by removing those lines related to hashCode(), equals()) and use the -t option when generating the spring boot server stubs.
Related
I am trying to implement a Spring Boot REST API but I was asked to use a interface as dependency but no impl, and I don't know how to achieve this. The way I implemented was to have service classes for my entities and there I would just call the repository in my methods. I would like an example of implementation like this.
I watched some youtube tutorials but they all used impl classes
Your controller should have a field of your interface type, with the injecting annotation (in spring it's #Autowired). The DI framework will do the heavy-lifting on startup and inject the correct implementation at runtime
#Controller
public class MyController {
#Autowired
private MyInterface myInterface;
....
}
For this to work, your framework needs to recognize the concrete class. In spring you can achieve this in multiple ways - scanning package paths, xml configuration files and more.
Check the spring documentation to see which way suits you best
I am using springboot and added swagger 3.0.0 dependency
When I run swagger ui model class is showing empty on ui and documentation pages.
I am using annotation to define swagger
If you are referring to Example Value Schema, please check your required object if you have getters and setters implemented.
Is there an existing Spring org.springframework.core.type.filter.TypeFilter implementation for Method annotations?
I am looking to call ClassPathScanningCandidateComponentProvider#findCandidateComponents in order got get all of the components that have an annotation or inherited annotations so I can validate the state of the configuration supports the annotation attribute values.
There is an org.springframework.core.type.filter.AnnotationTypeFilter, is there a similar implementation but getting classes that have methods that are annotated with the annotation?
If not is there a mechanism to search for components with methods with a specified annotation?
I have a spring-boot project. Some of the classes I am using it in the 'spring' way, meaning that they are annotated by "#Service", "#Repository", "#Autowired". At the same time, I have lots of classes, which are only used in the normal Java way, meaning that there are no any Spring annotations, and they are created in the standard way of constructing an object in a constructor.
For example, one of the non-annotated classes is:
public class GenericTree<T>
{
private GenericTreeNode<T> root;
public GenericTree ()
{
root = null;
}
public GenericTreeNode<T> getRoot ()
{
return this.root;
}
public void setRoot (GenericTreeNode<T> root)
{
this.root = root;
}
...
}
Is it OK or normal to have a mixure of classes with or without Spring annotations? Probably, I could convert all non-annotated classes into annotated classes by using Spring's annotation markers. Does that really benefit or is it necessary?
BTW, my application's main logic and functions are not web-centric, although they are created as a Spring project. The reason I created in Spring is I want to provide a restful service for my interface so that I can easily test in browser in development, and others can use it with Restful service.
Yes it is ok.
Keep in mind that annotations are not Spring exclusive. Annotations were introduced in Java 5 and they are just meta data for your Java code. This meta data can be useful at:
Compile time
Build time
Runtime
You can even create your own custom annotations and annotate your code with them.
Spring framework comes with some annotations and each one of them has its purpose, but that doesn't mean you have to annotate all your classes with Spring annotations when you are using this framework.
When you annotate your classes as Spring Beans, they become part of the Spring Application Context, thus making them available to be injected with the #Autowired annotation (Spring framework is based on the dependency injection design pattern). But Spring annotations have other implications too, I cannot go into the detail of each one of them but for example, you have to consider that the default scope of annotations like #Bean, #Component, #Controller, #Repository, #Service is Singleton. So whenever you annotate a class with one of these annotations and you don't define a scope, what you get is a singleton class shared all over your application. Other scopes are:
singleton
prototype
request
session
application
websocket
Taking in consideration your GenericTree class, does it make sense to annotate an abstract data structure class as a Spring Bean? Probably not.
So yes, when you develop an application based on Spring framework the normal thing is to have a mixture of Spring annotated classes and regular POJO's.
I recommend you to read the Spring framework documentation, learn what dependency injection is and the purpose and implications of the most used Spring annotations.
I'm attempting to transpile Java model classes to JavaScript using JSweet. The model classes contain JPA annotations like #Column. The transpilation fails as soon as it encounters import javax.persistence.Column.
The JPA annotations are irrelevant in JavaScript and should not be transpiled. Can this be done without changing the Java code?
More generally, is there a way to have JSweet ignore import statements, e.g., when all references to the imported packages are in #Erased methods?
Normally, JSweet just erases unknown annotations, so your code should transpile fine.
First thing to check: do you have a JPA jar in your classpath or in your Maven dependencies? JSweet uses javac, which requires all types to be in the classpath. I guess that the #Column annotation should be in there: https://mvnrepository.com/artifact/javax.persistence/persistence-api/1.0.2
As for the second part of your question, JSweet v2 provides an API to tune the generation of the code. See the specs. In the PrinterAdapter API, you can override the needsImport method to return null when the import is not needed. However I believe that you don't need this for your case since annotations are erased automatically.