I am writing a modular project where I need to integrate the swagger on different resources which uses the common controller.
So is it possible to generate the swagger UI for different resources manually writing code without annotation? If so can you please add an example of how can it be done?
Example:
Controller {
GET /{resource-name}
}
APIs:
GET /Patient
GET /Employee
GET /Student
Now I have one controller but 3 different resources. how can I have 3 different APIs in swagger?
SpringBoot + Java + Swagger
Thanks and regards,
Suraj Jannu
Related
In a very big Java app with lot's of DTOs, I want to export to an external project only the DTOs that are relevant to REST calls, and even better to export a part of them (the minimum required for REST calls).
The project uses Swagger and I am wondering if it is possible to take the output of Swagger (uses Java DTOs to create JSON\YAML files), which have the exact content I need, as an input to generate new Java DTO files. The generated files will be only those needed for REST and I will be able to easily export them.
Is this possible?
If not, what is the best approach to do that?
Maybe check out swagger.generator.io where you can generate a whole client library (including DTO classes) for your specified swagger definition file. For documentation please refer to their github page. You can also do the generation of the API client locally utizing the swagger codegen tools.
I am new to swagger and need to configure multiple controllers in spring boot swagger UI. But getting some issues as I am not able to resolve OR class/method in swagger when using with regex. Please let me know how can I resolve or keyword here. The basic need for me is I need to configure more than one controller paths in swagger UI.
Converting my comment to answer so that solution is easy to find for future visitors.
You are actually trying to use ant matchers in regex construct moreover you don't really need to use or method here.
You can use:
.paths(PathSelectors.regex("^/(test-data|bkc-user-details).*$"));
To match these 2 URI paths. If you want you can add more alternations in future this way.
I am working on 2 projects, one web app (Spring MVC) and one standalone backend service application (Spring boot) that heavily interact together. I am using hibernate for both and they are both coded using the Netbeans IDE.
My "issue" is that i end up with duplicate code in both project, mainly in the Repository and Service layers. My entities are obviously also duplicated since both projects use the same database.
Is there a way to make some sort of class library (a third project maybe?) and put all the common code in there? If that is indeed possible, how do you then change each project so they can still access this code as if it were part of them? I was thinking of putting all my Repositories, Services and entities in there to avoid code duplication and greatly reduce the risk of error.
Thank you!
Separate those Repository and Service classes to a submodule.
The structure looks like:
-- your app
-- api (dependent on `common` module)
-- webapp (dependent on `common` module)
-- common
Then the problem is to initialize beans inside common module. AFAIK, you have two options:
In #Configuration class of api or webapp module, add base packages of common module to component scan packages
In api or webapp resources folder, add Spring configuration factory
/src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=your.path.AutoConfiguration
Define service/repository #Bean inside AutoConfiguration class
I am assuming in this answer your projects are connected to each other
You can set multiple properties within one Spring project, where you store your database connection parameters etc. with the help of multiple property files.
For example:
application-web.properties
application-backend.properties
You can use these in your project, by activating the needed properties file per application. The profile names will be web and backend in these cases.
When using maven, this is the command line I am using:
mvn spring-boot:run -Drun.profiles=<<profile>>
Now, back to your java code.
If there are classes only one of your application is using, you can specify this by 'profile'. Example:
#Controller
#Profile({ "web" })
public class WebEndpoint {
}
This way you can make the shared code available for both applications, without duplicating most of the code.
I have an existing project having java code for Rest Web Services. I integrated Swagger to the java project which in turn added lot of annotations and descriptions, messing up my existing code. Is there a way to integrate swagger and read values from another class instead of messing up my existing class?
You just need to add the following annotation with you java file:
#Api(value = "myAdmin", description = "Operation pertaining Admin UI")
Other things are taken care by the RestContoller and all byt itself.
This annotation is not at all messing up with your code.
I'm currently working on a project that involves building a REST api using JavaEE. The setup of the project is Tomcat, Hibernate, Wink and Jackson Json for the different json-views. At the moment the unit testing of the rest resources is very poor, we've written custom classes that using introspection find the method that corresponds to a given resource but it is getting in our way (all the workarounds that we need to do in order to execute a simple unit test). I did a little research and found this.
My question is how to "install(add)" the MockServletInvocationTest class and its dependencies to the project? We're not using Maven, nor Spring. Is there a way to use Spring modules (I think this mock class is in the Spring test-module) outside Spring and if yes, how?
I found a solution, so for those who are interested: just add the following jars to your WebContent/WEB-INF/lib:
spring-test-xx.jar
spring-core-xx.jar
wink-component-test-support-xx-incubating.jar
commons-logging-xx.jar
And everything workds fine.