Category Archives: Java

MyBatis dynamic “union all”

Take a look at the following union sql fragment. What if includeFoo is false? You will end up with union all (select * from bar) , which is not legal sql. There must be a result set before “union all” The solution is to use <trim/> tag It means: if what’s inside <trim/> starts with […]

Use Flyway in a multi-module gradle project

Requirements Some of the sub-projects need to run flyway as a gradle task We want a single command line to do the flyway migrations for all the sub-projects mentioned above Solution Apply flyway plugin in these sub-projects Config flyway in these sub-projects Create a root-project task to call the flyway tasks in the sub-projects Code […]

Make sure chromedriver can run with Java selenium code, both in a real machine and in docker

Install chromedriver You want to make the following line of code run successfully. In windows, you need to download chrome driver from https://chromedriver.chromium.org/downloads and put it in system’s path (An alternative is to set the executable file’s path as a system property “webdriver.chrome.driver”, not beautiful) In *nix or linux-based Docker, you can Options needed for […]

Springfox for 204 response

What you need to, Return ResponseEntity<Void> Set the @ResponseStatus for your rest controller’s method If you don’t do that, you will get ResponseEntity as the response class in your swagger json, and you will get both 200 and 204 as your successful response code, which is not correct because your method only returns 204, no […]

Jetty’s start.jar forks a new JVM to run the app if logback module is enabled

Jetty is too smart. If there is any JVM option provided, Jetty will spawn a new JVM instance to do the real work, even if you don’t use “–exec” option in start.ini And jetty’s logback module introduces a JVM option. See here You will see: -Dorg.eclipse.jetty.util.log.class?=org.eclipse.jetty.util.log.Slf4jLog So does it matter? It matters a lot because […]

Code snippet: Swagger + Spring Boot

compile ‘io.springfox:springfox-swagger2:2.9.2’ compile ‘io.springfox:springfox-swagger-ui:2.9.2’ /** See https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Value(“${enable.swagger}”) //You may want to disabled it in PROD boolean enableSwagger; @Bean public Docket api() { Docket api = new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(“your.package”)) .paths(PathSelectors.any()) .build(); ApiInfo apiInfo = new ApiInfoBuilder() .version(“xxx”) .description(“xxx”) .termsOfServiceUrl(“xxx”) .title(“Xxx Doc”) .build(); api.useDefaultResponseMessages(false).apiInfo(apiInfo); api.enable(enableSwagger); return api; } […]

Generate PDF from server side using Java

You can use OpenPDF to draw a PDF (the open source fork of iText). However, the learning curve of creating PDF with layouts and stuff is very high. A more cost-effective solution is create an HTML version of it and convert it using Flying Saucer with its OpenPDF component . As a Java developer you […]