Java

Jersey Client: What if the server side doesn’t return JSON ContentType when it is supposed to?

Your server says it will return JSON string as response, but the ContentType header it sends is not "application/json" but "text/html". What will happen?   Your client will see an exception saying "A message body reader for … was not found" .  What you need to do is to have your own "provider" and let …

Jersey Client: What if the server side doesn’t return JSON ContentType when it is supposed to? Read More »

How to enable log4j when doing spring-based integration test?

Just put a log4j.xml on test’s class path and log4j will be activated. However, you may hate to have 2 "log4j.xml" in your eclipse. You want the log4j config for testing to be called log4j-test.xml. In that case, do the following: //create your own runner, withen which load log4j public class MySpringJunit4ClassRunner extends SpringJUnit4ClassRunner { …

How to enable log4j when doing spring-based integration test? Read More »

Web Application: Don’t send all the stack trace of an exception to client if Apache Httpd is used as reverse proxy

Don’t send all the stack trace of an exception to client if Apache Httpd is used as reverse proxy.  Otherwise, it may return Http 502 to your client.   It may say the following just because the response from your servlet container(tomcat etc.) is too big in size:  502 Bad Gateway The proxy server received …

Web Application: Don’t send all the stack trace of an exception to client if Apache Httpd is used as reverse proxy Read More »

The best way to deal with CORS issues with Swagger UI

You can set CORS filter on your web.xml or on your tomcat’s web.xml, like this. CorsFilter org.apache.catalina.filters.CorsFilter CorsFilter /*  However, 1. Your system gets insecure because of this, especially on PROD site where you don’t want someone to invoke your RESTFul services with swagger ui.  2. There may still be CORS javascript bugs in Swagger …

The best way to deal with CORS issues with Swagger UI Read More »

Java Time Zone: It’s 9 o’clock here, what’s your time ?

private static void printTimeMain() throws ParseException { // the computer which runs this program is of the time zone of China long someSummerMilis = DateUtils.parseDate(“2015-7-4 16:00:00”, new String[] { “yyyy-MM-dd HH:mm:ss” }).getTime(); long someWinderMilis = DateUtils.parseDate(“2015-11-11 16:00:00”, new String[] { “yyyy-MM-dd HH:mm:ss” }).getTime(); System.out .println(“==============when it is 2015-7-4 16:00:00 in China, the world’s times are==============”); …

Java Time Zone: It’s 9 o’clock here, what’s your time ? Read More »

How to let MyBatis annotation to set the id and timestamp properties of the JavaBean I passed in ?

If you are with a auto-generated key, when you carry out a method like this public void saveNewUser(User user); what do you expect besides a new record in DB?   You want the user.id also has been set after the execution. That’s what real ORM does With MyBatis, you can do it like the following. …

How to let MyBatis annotation to set the id and timestamp properties of the JavaBean I passed in ? Read More »

Email sending using others’ smtp server should done asynchronously

SMTP access provided by 3rd-party sponsors such as Godaddy can be very unreliable. It may take seconds to setup a TCP connection or read and write data.  As a result, the thread of sending an email may be blocked.  In return, if this thread is a user thread, i.e., created to serve a user’s request, …

Email sending using others’ smtp server should done asynchronously Read More »