Skip to content

Instantly share code, notes, and snippets.

@baberali
baberali / gist:f61a3293746b499da6b5e8d27838b2da
Created May 9, 2017 06:14
Get control and handle error condition with custom exception when spring/jackson is unable to parse invalid Enum value
// put this method in the enum
// it will be called by jackson when mapping a value to enum
@JsonCreator
public static EnumType fromString(String string) {
EnumType enumType = null;
try {
enumType = EnumType.valueOf(string);
} catch (Exception e) {
new MyRuntimeException(string);
}
@baberali
baberali / LoggingFilter.java
Created August 15, 2016 11:48
Instead of writing your own classes to cache request response for logging, Spring provides a couple of useful classes i.e. ContentCachingRequestWrapper and ContentCachingResponseWrapper. These classes can be utilized very effectively, for example, in the following little filter:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;