I am absolutly new in Swagger and I have the following doubt:
I know that gnerally I have to create Swagger document before code my REST API and the use this document to create my API (I know that from the Swagger Editor I can also generate my API server automatically).
My problem is the following one:
I am working on a Java application (a Spring Boot application) that implements my REST API. I want to use Swagger to create my API documentation.
Exist a way to automatically do it? From my Java code to the Swagger yaml file? For example annoting my Java code in some way that could be parsed by some tool?
Yes, there is a tool which can easily generate the swagger documentation from the code you have already written.
This included into a Spring Appllication will create the documentation
https://springfox.github.io/springfox/docs/current/
To my mind this is the right way to it. Don't create a documentation and have the code generated, rather generate the documentation. That's the way Javadoc is created as well.
You don't need to annotate nothing in particular.
Create a class similar to the following to access the API with Swagger:
#Configuration
#EnableSwagger2
public class SwaggerConfig{
#Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2).select().apis(
RequestHandlerSelectors.basePackage("com.yourcompany.restcode")).paths(PathSelectors.any()).build();
}
}
Then access the API documentation with something like:
localhost:8080/swagger-ui.html#/
Related
I am trying to find way, how to call external API based on YAML definition:
I have (Spring boot, external) API-1, generated with OpenAPI plugin, which has
it's YAML file.
I have (Spring boot) API-2, which calls API-1. But all I have is YAML definition of API-1.
We know external API can be called with:
#Bean
public WebClient remoteApiBean() {
return WebClient.create("http://example.com/api");
}
And e.g. Service implementation:
#Autowired
public MyService(WebClient remoteApiBean) {
this.remoteApiBean = remoteApiBean;
}
And requesting resource
this.remoteApiBean.get()
.uri("/request")
.retrieve()
.bodyToMono(ModelDTO.class)
.block(REQUEST_TIMEOUT);
API and model info are stored inside of API-1 YAML config. My question is, if it is possible to use API-1 YAML definition as a resource for API-2. Generate code above and also generate DTOs. I think, there MUST be any plugin which generate this, but I am unable to find it, because there are very similar issues with Spring Boot yaml external configurations and google result are bad according this searching.
I am new to Java so apologies if this is a simple thing. I have built decorators in Python for authorizing RESTFul endpoints in Flask and have just built my first Java Webserver but am unable to figure out how to create a similar decorator in Java.
I want to do some pre-checks before running the method (i.e. is the user allowed to access this route). Ideally this would be a decorator like #authorize that, if authorized, will execute the method, but if unauthorized then it would through a 403 error instead.
#Path("/")
public final class HelloWorld {
#GET
#Path("/hello")
#authorize // How would I implement this?
public String sayHelloWorld() {
return "Hello World!";
}
}
EDIT: I am using Grizzly as the web Framework and I will be using an external Policy Management System (Apache Ranger) for managing authorization.
First of all: defining such custom annotations is exactly how you can approach such things in Java. The JAX-RS specification provides all the things you need for such kind of method binding.
The thing that is slightly more complicated: how to nicely do that for the framework that you are using.
With JAX-RS and Jersey for example, creating your own annotations is well documented. And Jersey might be a good starting point, as that is simply a straight forward way to get JAX-RS working.
So, first you start by learning how to use Jersey in general, for example from vogella. Next: you can start to add your custom annotations, see here for an example.
There is even an existing question about using custom annotations for access validation.
I am using swagger to document my REST API. But i am using swagger annotation in my code like #API, #APIOperation etc. But i dont want to add these annotation. Is there any way to generate YAML or JSON file for REST API then use these files to describes, document and visualize REST API.
The YAML/JSON is just a documentation, you cant implement them by including this file, but you can provide this documentation e.g {yourAPI}/swagger.yaml.
I want to replace Basic Authentication for Spring Cloud Config Server with oAuth implementation. Let's leave Config Server alone for now and focus on changes for Config Client. Obviously I don't want to write my own implementation for whole thing, but instead execute my own logic and fallback on standard Config Client. Also I have to pack my changes into library since I will use it in multiple micro-services.
Long story short I want to achieve following:
1a. Create custom Starter which will contain Spring Cloud Config Client as dependency. Is it even doable or necessary?
or
1b. Create custom Starter with only my custom logic which will be executed before Spring Cloud Config Client. In this case each micro-service will have Spring Cloud Config Client and custom Starter as dependencies. How can I manage execution order and inject custom logic results into Config Client?
2.Introduce new bootstrap settings. e.g. spring.cloud.config.custom.username and spring.cloud.config.custom.password (Optional).
3.Introduce custom annotation for custom Starter. e.g. #enableCustomConfigClient (Optional).
I started with building custom Starter with following code in /resources/META-INF/spring.factories:
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.greeter.config.ConfigClientBootstrapConfiguration
But this code invoked after profile is set, not the first thing like Config Client does.
Any suggestions and especially code samples are appreciated. Thanks!
Posting approach I chose for future reference.
Create new package which will be executed on top of / before Spring Cloud Config Client. Two main features here:
Create file src/main/resources/META-INF/spring.factories with org.springframework.cloud.bootstrap.BootstrapConfiguration={YOUR_CLASS}
In {YOUR_CLASS} apply custom logic. Don't forget to use #org.springframework.core.annotation.Order({YOUR_PRECEDENCE}) and fact that Ordered.LOWEST_PRECEDENCE will be executed first
Build jar from previous step and include it into your project (as local file or via artifactory)
Add Custom logic to Spring Cloud Config Server so it can use JWT.
Working example is here: https://github.com/ka4ok85/spring-cloud-config-client-jwt
the problem file XML configurations spring framework and JAVA can not change ? while in others they rather use now JSON ...
I guess you want to have a spring configuration using annotation. Follow link :
https://www.mkyong.com/spring3/spring-3-javaconfig-example/
And have JSON from java class. Link:
https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/