How to exclude lib in Swagger API documentation - java

I have used keycloak-admin-client in a jaxrs application, and use swagger to generate API documentation. Swagger generates documents for all methods in keycloak-admin-client. How can I exclude this library from documentation in swagger ?
Dependency:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-jaxrs2-servlet-initializer</artifactId>
<version>2.1.0</version>
</dependency>

by this solution assign only my package for scan
#ApplicationPath("/api")
public class ApplicationInitializer extends Application {
public ApplicationInitializer(#Context ServletConfig servletConfig) {
super();
OpenAPI oas = new OpenAPI();
try {
Set<String> resource = new HashSet<>();
resource.add("my.company.api.path");
SwaggerConfiguration oasConfig = new SwaggerConfiguration()
.openAPI(oas)
.prettyPrint(true)
.resourcePackages(resource);
OpenApiContext oac = new JaxrsOpenApiContextBuilder()
.servletConfig(servletConfig)
.application(this)
.openApiConfiguration(oasConfig)
.buildContext(true);
oac.read();
} catch (OpenApiConfigurationException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}

Related

Creating an Amazon S3 bucket Using the AWS SDK for Java : Exception in thread "main" java.lang.NoClassDefFoundError

Getting below error while trying to create an s3 bucket of aws using java API :
Error : Exception in thread "main" java.lang.NoClassDefFoundError: software/amazon/awssdk/protocols/query/internal/unmarshall/AwsXmlErrorUnmarshaller at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlErrorTransformer.(AwsXmlErrorTransformer.java:40) at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlErrorTransformer.(AwsXmlErrorTransformer.java:34) at software.amazon.awssdk.protocols.xml.internal.unmarshall.AwsXmlErrorTransformer$Builder.build(AwsXmlErrorTransformer.java:113) at software.amazon.awssdk.protocols.xml.AwsXmlProtocolFactory.createErrorTransformer(AwsXmlProtocolFactory.java:135) at software.amazon.awssdk.protocols.xml.AwsS3ProtocolFactory.createErrorCouldBeInBodyResponseHandler(AwsS3ProtocolFactory.java:80) at software.amazon.awssdk.services.s3.DefaultS3Client.createBucket(DefaultS3Client.java:1144) at com.act.niti.main(niti.java:33) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk.protocols.query.internal.unmarshall.AwsXmlErrorUnmarshaller at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ..
Code :
try {
Region region = Region.US_EAST_2;
S3Client s3 = S3Client.builder()
.region(region)
.build();
S3Waiter s3Waiter = s3.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket("abc")
.build();
s3.createBucket(bucketRequest); //creating s3 bucket
System.out.println("bucket........abc");
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket("abc")
.build();
// Wait until the bucket is created and print out the response
WaiterResponse<HeadBucketResponse> waiterResponse =
s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println("abc" +" is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
Note : Using java 8
POM Xml :
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.570</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.17.269</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ec2</artifactId>
<version>2.5.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-gamelift -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-gamelift</artifactId>
<version>1.11.647</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-transfer-manager</artifactId>
<version>2.17.103-PREVIEW</version>
</dependency>
<dependencies>
Looks like you are mixing up V1 and V2 in your POM file. You have
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.570</version>
</dependency>
There is no need for this Dep when using AWS SDK for V2. S3Client is a V2 Service Client. In fact -- your errors are most likely related to mxing up SDK versions in your POM file.
The POM file that you should use can be found in the AWS Github repo here:
https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3
If you are not familiar with AWS SDK for Java v2, refer to the DEV Guide:
https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/home.html
The V2 Java code that works when you use the proper POM file is here:
package com.example.s3;
// snippet-start:[s3.java2.create_bucket_waiters.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketRequest;
import software.amazon.awssdk.services.s3.model.HeadBucketResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.waiters.S3Waiter;
import java.net.URISyntaxException;
// snippet-end:[s3.java2.create_bucket_waiters.import]
/**
* Before running this Java V2 code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class CreateBucket {
public static void main(String[] args) throws URISyntaxException {
final String usage = "\n" +
"Usage:\n" +
" <bucketName> \n\n" +
"Where:\n" +
" bucketName - The name of the bucket to create. The bucket name must be unique, or an error occurs.\n\n" ;
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
}
String bucketName = args[0];
System.out.format("Creating a bucket named %s\n", bucketName);
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder()
.region(region)
.credentialsProvider(credentialsProvider)
.build();
createBucket (s3, bucketName);
s3.close();
}
// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket( S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket(bucketName)
.build();
// Wait until the bucket is created and print out the response.
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName +" is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
// snippet-end:[s3.java2.create_bucket_waiters.main]
}

Found interface org.apache.poi.util.POILogger, but class was expected error

public String readExcel(String columnname,String UserType) {
try {
FileInputStream file = new FileInputStream(path);
#SuppressWarnings("resource")
XSSFWorkbook wr = new XSSFWorkbook(file);
XSSFSheet sh = wr.getSheet(prop.getProperty("env"));
int row = sh.getPhysicalNumberOfRows();
System.out.println(row);
for(int j=0;j<row;j++) {
if((sh.getRow(j).getCell(1).getStringCellValue()).equalsIgnoreCase(UserType)) {
for(int i=0;i<row;i++) {
String s=sh.getRow(0).getCell(i).getStringCellValue();
if(s.equals(columnname)) {
value = sh.getRow(0).getCell(i).getStringCellValue();
}
}
}
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return value;
}
public static void main(String args[]) {
Util obj=new Util();
obj.readExcel("Username","Testuser1");
}
I am using this code to read data from Excel, but getting an exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface org.apache.poi.util.POILogger, but class was expected.
Not sure about the reason, can anyone help me with this, please?
Add this in your pom.xml file
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>x.x.x</version>
</dependency>
The version of poi-ooxml and poi should be the same.
I am assuming that you already have poi in your pom.xml file
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>x.x.x</version>
</dependency>
Hope this helps
I got this error when the versions of poi and poi-ooxml did not match. This caused the error:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
The error went away after aligning the versions of these libraries:
<properties>
<poi.version>4.1.2</poi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>

Generate json schema from POJO

I need to generate json schema from my POJOs. The requirement is that every POJO must be exported as a separate file and the references inside the json schema must be handled appropriately. It means that the library should keep track of which POJO is exported to which file. I found this library: https://github.com/mbknor/mbknor-jackson-jsonSchema and it works fine but it seems (or at least i cannot find such option) that i can't accomplish the requirements without custom coding. Do you know any other library that supports this?
You can use Jackson to generate the JSON schema using the following maven dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jsonSchema</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
You can then generate the schema by writing something like this
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
Reflections reflections = new Reflections("my.pojo.model",new SubTypesScanner(false));
Set<Class<?>> pojos = reflections.getSubTypesOf(Object.class);
Map<String, String> schemaByClassNameMap = pojos.stream()
.collect(Collectors.toMap(Class::getSimpleName, pojo -> getSchema(mapper, schemaGen, pojo)));
schemaByClassNameMap.entrySet().forEach(schemaByClassNameEntry->writeToFile(schemaByClassNameEntry.getKey(),schemaByClassNameEntry.getValue()));
}
private static void writeToFile(String pojoClassName, String pojoJsonSchema) {
try {
Path path = Paths.get(pojoClassName + ".json");
Files.deleteIfExists(path);
byte[] strToBytes = pojoJsonSchema.getBytes();
Files.write(path, strToBytes);
}catch (Exception e){
throw new IllegalStateException(e);
}
}
private static String getSchema(ObjectMapper mapper,JsonSchemaGenerator schemaGenerator,Class clazz){
try {
JsonSchema schema = schemaGenerator.generateSchema(clazz);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);
}catch (Exception e){
throw new IllegalStateException(e);
}
}

XA resource has become unavailable XID raised -7

I am getting below error in a random way. It doesn't throw error initially but after a while the error is thrown. Once I restart my server everything gets reset and error is not thrown.
XA resource 'XXXXAtomikosDataSource': resume for XID '31302E3137382E36382E3235302E746D30303031353030303138:31302E3137382E36382E3235302E746D3135' raised -7: the XA resource has become unavailable
I am using below dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
And with configuration beans
#Bean(name= "userTransaction")
public UserTransaction userTransaction() throws Throwable {
UserTransactionImp userTransactionImp = new UserTransactionImp();
return userTransactionImp;
}
#Bean
#Primary
public TransactionManager atomTransactionManager() throws Throwable {
UserTransactionManager userTransactionManager = new UserTransactionManager();
userTransactionManager.setForceShutdown(false);
return userTransactionManager;
}
#Bean("atomikosTransactionManager")
public PlatformTransactionManager platformTransactionManager() throws Throwable {
UserTransaction userTransaction = userTransaction();
TransactionManager transactionManager = atomTransactionManager();
JtaTransactionManager jtaTransactionManager = new JtaTransactionManager(userTransaction, transactionManager);
jtaTransactionManager.setAllowCustomIsolationLevels(true);
jtaTransactionManager.setDefaultTimeout(1000);
return jtaTransactionManager;
}
#Bean(name = "atomikosDataSources")
public Map<String, DataSource> atomikosDataSources(PropertiesFactoryBean databaseProperties, Environment environment, BcusDefaultEncrytor bcusDefaultEncrytor) throws SQLException, IOException {
List<String> databases= Collections.list(databaseProperties.getObject().propertyNames()).stream().map(key->key.toString()).filter(key-> key.contains("url")).collect(Collectors.toList());
Map<String, DataSource> collect = databases.stream().collect(Collectors.toMap(dbName -> dbName.split("\\.")[0].toUpperCase() + "AtomikosDataSource", dbName -> {
OracleXADataSource dataSource = null;
try {
dataSource = new OracleXADataSource();
dataSource.setURL(environment.getProperty(dbName));
dataSource.setUser(environment.getProperty("database.username"));
dataSource.setPassword(bcusDefaultEncrytor.decryptQAPassword(environment.getProperty("database.password")));
}
catch (SQLException e)
{
LOG.error(
"DatabaseConditionException [errorCode= Error while configuring atomikos data sources- Could not OracleXADataSource object, errorMessage= {} ]",
e.getMessage());
}
catch (Exception e) {
LOG.error(
"DatabaseConditionException [errorCode= Error while configuring atomikos data sources- Could not decrypt QA Password make sure you have proper **QA** Encryption password in your folder, errorMessage= {} ]",
e.getMessage());
}
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(dataSource);
xaDataSource.setUniqueResourceName(dbName.split("\\.")[0].toUpperCase() + "AtomikosDataSource");
xaDataSource.setPoolSize(5);
return xaDataSource;
}));
return collect;
}
The issue is because of a bug in atomikos 3.9.3 version in spring-boot-starter-jta-atomikos brings:1.4.2.RELEASE.
Once you update the version to atomikos 4.0.4, the issue resolves.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
<exclusions>
<exclusion>
<artifactId>transactions-jdbc</artifactId>
<groupId>com.atomikos</groupId>
</exclusion>
<exclusion>
<artifactId>transactions-jms</artifactId>
<groupId>com.atomikos</groupId>
</exclusion>
<exclusion>
<artifactId>transactions-jta</artifactId>
<groupId>com.atomikos</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jms</artifactId>
<version>${transaction.version}</version>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jta</artifactId>
<version>${transaction.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.0.1B_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.atomikos</groupId>
<artifactId>transactions-jdbc</artifactId>
<version>${transaction.version}</version>
</dependency>

Sending HTML email with Spring Boot and Thymeleaf

I am checking out how to send an email using Spring Boot.
Send an e-mail using standard Spring Boot modules and prepare HTML content for a message using Thymeleaf template engine.
This is the dependencies I use
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Here my MailClient
#Service
public class MailClient {
private JavaMailSender mailSender;
private MailContentBuilder mailContentBuilder;
#Autowired
public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
this.mailSender = mailSender;
this.mailContentBuilder = mailContentBuilder;
}
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("amadeu.cabanilles#gmail.com");
messageHelper.setTo("amadeu.cabanilles#gmail.com");
messageHelper.setSubject("Sample mail subject");
String content = mailContentBuilder.build(message);
messageHelper.setText(content, true);
};
try {
mailSender.send(messagePreparator);
} catch (MailException e) {
e.printStackTrace();
}
}
}
This is my Test Class
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(Application.class)
public class MailClientTest {
#Autowired
private MailClient mailClient;
private GreenMail smtpServer;
#Before
public void setUp() throws Exception {
smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
smtpServer.start();
}
#Test
public void shouldSendMail() throws Exception {
//given
String recipient = "amadeu.cabanilles#gmail.com";
String message = "Test message content";
//when
mailClient.prepareAndSend(recipient, message);
//then
String content = "<span>" + message + "</span>";
assertReceivedMessageContains(content);
}
private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
String content = (String) receivedMessages[0].getContent();
System.out.println(content);
assertTrue(content.contains(expected));
}
#After
public void tearDown() throws Exception {
smtpServer.stop();
}
}
Executing the Test in my computer is OK, I pass the test but I don't receive any email.
You don't receive any email because this integration test uses local testing SMTP server stub - GreenMail. The test doesn't send real emails, only verifies if the mail is prepared and sent correctly if a real SMTP server is available in the production.
In order to send emails from your local environment, you need to setup some SMTP server, but then, automated verification if the mail is actually sent is a completely different story.

Categories