Not working added global header parameters using Swagger 3 UI - spring-boot

I have migrated existing project Swagger to Swagger3 using dependency springdoc-openapi-ui 1.6.8 version .
Getting issue while added the global header parameter in Swagger config file, it was not showing at Swagger dashboard
Please advised me if any issue in mentioned code.
Code:
**
#Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components()
.addSecuritySchemes("basicScheme",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic"))
.addParameters("myHeader1",
new Parameter().in("header").schema(new StringSchema()).name("myHeader1"))
.addHeaders("myHeader2",
new Header().description("myHeader2 header").schema(new StringSchema())))
.info(new Info().title("eWallet API Sandbox").description("eWallet API Sandbox").version("v1.0")
.contact(new Contact().name("WOW Finstack").url("https://wowdigital.ai/")
.email("info#wowdigital.ai"))
.termsOfService("WOW Finstack").license(new License().name("License").url("#")));
//
};
**
Dependency :
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>

I recently upgraded to springdoc-openapi-ui and struggled a bit to make global headers work on Spring boot 2.6.3.
I managed to make global header work if it's defined as a Parameter (using new Parameter()...) but I did not make it work when defined as a Header (using new Header()...)
I guess that you already defined a GroupedOpenApi Spring Bean. So what you have to do is add a OpenApiCustomiser to this GroupedOpenApi, see addOpenApiCustomiser(globalHeaderCustomizer()) below:
#Bean
public GroupedOpenApi publicGroup() {
return GroupedOpenApi.builder()
.packagesToScan("com.my.package")
.pathsToMatch("/**")
.group("public")
.addOpenApiCustomiser(globalHeaderCustomizer()) // --> you need this!
.build();
}
where globalHeaderCustomizer() is:
private OpenApiCustomiser globalHeaderCustomizer() {
return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
.forEach(operation -> operation.addParametersItem(
new HeaderParameter().$ref("#/components/parameters/myHeader1")));
}
I think that should fix your issue.

Related

Autowiring MongoClient and MongoClientSettings without explicitly specifying a Connection String

I am upgrading the MongoDB driver which requires moving away from the older MongoClientOptions to the newer MongoClientSettings.
In the older implementation, the following configuration was used within a #Configuration class with the ConnectionString inferred from the spring.data.mongodb.uri and an #Autowired MongoTemplate:
#Bean
public MongoClientOptions mongoOptions() {
Builder clientOptionsBuilder = MongoClientOptions.builder()
//Timeout Configurations
if(sslIsEnabled) {
clientOptionsBuilder.sslEnabled(true)
//Other SSL options
}
return clientOptionsBuilder.build();
}
And in the Newer Implementation, a ConnectionString parameter is specifically expected, and the property file spring.data.mongodb.uri is not selected automatically. As a result, I have specified the connection string using the #Value Annotation. Not doing this results in the program to infer localhost:27017 as the connection source.
#Value("${spring.data.mongodb.uri}")
String connectionString;
#Bean
public MongoClient mongoClient() {
MongoClientSettings.Builder clientSettingsBuilder = MongoClientSettings.builder()
.applyToSocketSettings(builder -> {
// Timeout Configurations
}).applyConnectionString(new ConnectionString(connectionString));
if (sSLEnabled) {
clientSettingsBuilder.applyToSslSettings(builder -> {
builder.enabled(sslIsEnabled);
//Other SSL Settings
});
}
return MongoClients.create(clientSettingsBuilder.build());
}
While documentation and other StackOverflow posts mention MongoClientSettings overrides the property file entries, is there a way to retrieve/infer the MongoClientSettings from the property files and then append other custom configurations to it?
I am using Spring Boot 2.6 and spring starter dependency for MongoDB
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
I found that a similar question asked on GitHub earlier.
Modify MongoClientSettings while using auto configuration with mongodb #20195
Replacing the #Bean configuration which had MongoClientOptions with MongoClientSettingsBuilderCustomizer helped solve the problem.
#Bean
public MongoClientSettingsBuilderCustomizer mongoDBDefaultSettings()
throws KeyManagementException, NoSuchAlgorithmException {
return builder -> {
builder.applyToSocketSettings(bldr -> {
//Apply any custom socket settings
});
builder.applyToSslSettings(blockBuilder -> {
// Apply SSL settings
});
// Apply other settings to the builder.
};
}

Swagger UI - Load custom file.yaml/json instead default configuration

I'm developing an SpringBoot REST project which runs perfectly. I'm trying to implement the OpenApi-ui in the project. It's working fine by default but I'd like to use my own yaml/json information file instead the default info.
I have been following the F.A.Q SpringDoc documentation , but nothing is working for me. It's throwing FAILED TO LOAD API DEFINITION : Fetch error undefined /open-api.yaml in the UI. Am I missing something in my configuration?
Thanks in advance.
Implementation
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.5.9'
App config (route yaml = src/main/resources)
springdoc:api-docs:enabled: false
swagger-ui:url: /open-api.yaml
Configuration
#Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components().addSecuritySchemes("basicScheme",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
.info(new Info().title("MyApp").version("1.0")
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
}
#Bean
public SpringDocConfiguration springDocConfiguration(){
return new SpringDocConfiguration();
}
#Bean
public SpringDocConfigProperties springDocConfigProperties() {
return new SpringDocConfigProperties();
}
Yaml file
openapi: 3.0.3
info:
title: MyApp
description: MyApp Description
version: 1.0.0
servers:
- url: http://localhost:8080
description: Local server
{...more}
Access URL to OpenApi UI
http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config
OpenApi UI Image
Just if someone is looking for something similar, we finally created a new class, extending SwaggerIndexPageTransformer and implementing by SwaggerIndexTransformer , which led us to use #override method to change the url.
You can follow > https://github.com/springdoc/springdoc-openapi/issues/763

Spring boot actuator breaks #AutoConfigureMockRestServiceServer

I have a class which builds multiple RestTemplates using RestTemplateBuilder:
private RestTemplate build(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.rootUri("http://localhost:8080/rest")
.build();
}
For my test setup I use #AutoConfigureMockRestServiceServer and mock responses using MockServerRestTemplateCustomizer:
mockServerRestTemplateCustomizer.getServer()
.expect(ExpectedCount.times(2),
requestToUriTemplate("/some/path/{withParameters}", "withParameters"))
.andRespond(withSuccess());
My test passes when I uncomment the spring-boot-actuator dependency in my pom and fails in the other scenario with the following message.
Expected: /some/path/parameter
Actual: http://localhost:8080/rest/pos/some/path/withParameters
I noticed by debugging through MockServerRestTemplateCustomizer that spring-boot-actuator applies a "DelegateHttpClientInterceptor" for supporting their built in metrics for rest templates. However this creates a problem with the following code which I found in RootUriRequestExpectationManager:
public static RequestExpectationManager forRestTemplate(RestTemplate restTemplate,
RequestExpectationManager expectationManager) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
if (templateHandler instanceof RootUriTemplateHandler) {
return new RootUriRequestExpectationManager(((RootUriTemplateHandler) templateHandler).getRootUri(),
expectationManager);
}
return expectationManager;
}
Because as mentioned above spring-boot-actuator registers a "DelegateHttpClientInterceptor" which leads to the above code not recognizing the RootUriTemplateHandler and therefore not matching the request using requestToUriTemplate.
What am I missing here to get this working?
As Andy Wilkinson pointed out, this seems to be a bug in Spring boot. I created an issue with a sample project.

Thymealeaf text template not printing the current date

I am using spring boot 2.2.4 and using the thymealeaf text templates for the first time with spring boot.
Belowis the texttemplate I am trying to use and I am trying to print the current date and time but it is printing blank on screen.
CurrentDate:[#th:block th:utext="${#temporals.format(now, 'dd/MMM/yyyy HH:mm')}"/]
I have added the Java8 date time dependency in pom.xml and also added the java8dialect in the template resolver bean.
pom.xml
--------
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
ThymeleafConfig.java
----------------------
#Configuration
public class ThymeleafConfig {
#Bean(name = "textTemplateEngine")
public TemplateEngine textTemplateEngine() {
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.addTemplateResolver(textTemplateResolver());
templateEngine.addDialect(new Java8TimeDialect());
return templateEngine;
}
private ITemplateResolver textTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("/templates/text/");
templateResolver.setSuffix(".txt");
templateResolver.setTemplateMode(TemplateMode.TEXT /* https://github.com/thymeleaf/thymeleaf/issues/395 */);
templateResolver.setCharacterEncoding("UTF8");
templateResolver.setCheckExistence(true);
templateResolver.setCacheable(false);
return templateResolver;
}
}
Can anybody please tell me what am I doing wrong in thymealeaf text template that it is not printing the date?
You can use a Thymeleaf utility method to print various date and time variants.
For example:
<div th:text="${#dates.createNow()}"></div>
I think that may answer your specific question.
However, I suspect your question is more about the wider topic of binding Java objects to Thymeleaf templates in Spring - and I have not used Spring with Thymeleaf.

How to configure Soap Webservices using Spring ws

I am new to the Spring ws.For the following things i need some clarification
1.I want to know how to customise the bindings,operations,port names etc..that is generated automatially by Spring.
2.How to specify multiple bindings,port types if we have multiple operations so that all should be generated in same wsdl.
You can customize the dynamic WSDL properties using DefaultWsdl11Definition bean
#Bean
public DefaultWsdl11Definition orders() {
DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
definition.setPortTypeName("Orders");
definition.setLocationUri("http://localhost:8080/ordersService/");
definition.setSchema(new SimpleXsdSchema(new ClassPathResource("echo.xsd")));
return definition;
}
Ref : http://docs.spring.io/spring-ws/docs/current/reference/html/server.html
API Doc : http://docs.spring.io/spring-ws/sites/1.5/apidocs/org/springframework/ws/wsdl/wsdl11/DefaultWsdl11Definition.html

Resources