Spring
Prerequisite
RESTful web service
- Build a Hello World REST service in less than 6 minutes
- Note: project-package type select : war file for deploying tomcat servlet api
- Web Services Simple Example
- Building a RESTful Web Service with Spring Boot Actuator
- How to use Log4j in spring-boot-starter
- Example pom.xml of log4j
Get the POST request body from HttpServletRequest
req.getReader().lines().collect(Collectors.joining());
Spring cache
Cache with key on multiple arguments
@CachePut(value="cache_goods", key="{#codeName, #g.gid}")
public Goods updateGoods (String codeName, Goods g) {
...
}
Get value from native cache instance
AnnotationConfigApplicationContext appCtx = AppContext.getCacheAnnotContext();
SimpleCacheManager cacheMgr = (SimpleCacheManager) appctx.getBean("cacheMgr");
GuavaCache guc = (GuavaCache) cacheMgr.getCache("cache_company");
ConcurrentMap<Object, Object> map = guc.getNativeCache().asMap();
String value = (String) map.get( Arrays.asList(token) );
Cache configuration with annotation
AppContext.java
public class AppContext {
private static AnnotationConfigApplicationContext _cache_annot_ctx = null;
private AppContext() { }
public static AnnotationConfigApplicationContext getCacheAnnotContext() {
synchronized (AppContext.class) {
if (null == _cache_annot_ctx) {
_cache_annot_ctx = new AnnotationConfigApplicationContext();
_cache_annot_ctx.register(CacheConfig.class);
_cache_annot_ctx.refresh();
}
}
return _cache_annot_ctx;
}
}
CacheConfig
@Configuration
@ComponentScan("org.venraas.hermes.apollo.*")
@EnableCaching
public class CacheConfig {
@Bean(name="cacheMgr")
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<GuavaCache> cache_list = new ArrayList<GuavaCache>();
GuavaCache cache_company =
new GuavaCache(
"cache_company",
CacheBuilder.newBuilder()
.maximumSize(Constant.CACHE_SIZE_10K)
.expireAfterWrite(30, TimeUnit.SECONDS)
.build());
cache_list.add(cache_company);
cacheManager.setCaches(cache_list);
return cacheManager;
}
CompanyClient.java
package org.venraas.hermes.apollo.raas;
...
@Service
public class CompanyClient {
@Cacheable(value="cache_company", key="{#token}")
public String getCodeName(String token) {
...
}
}
@PropertySource - read the values from property file
application.properties
conn.timeout=2000Config.java
public class Config { int conn_timeout; static Config _conf = new Config(); private Config() { } static public Config getInstance() { return _conf; } public int getConn_timeout() { return conn_timeout; } public void setConn_timeout(int ms) { this.conn_timeout = ms; } }@PropertySource
@Configuration @PropertySource("classpath:application.properties") public class AppProperty { @Value("${conn.timeout}") private String conn_timeout; @Autowired Environment env; /** * The Singleton instance creates as application startup automatically. * @return Config the singleton instance */ @Bean public Config getConfig() { Config cfg = Config.getInstance(); cfg.setConn_timeout(Integer.valueOf(env.getProperty("conn.timeout"))); return cfg; } }Bean usage
Config cfg = Config.getInstance() int timout = cfg.getConn_timeout();
Spring Data - JPA and PostgresSQL
Quick Tips
Request parameters validation
- Using JSR-303/JSR-349 Bean Validation API
- Adding @Valid @RequestBody to controller
- Validate a RESTful web service request in spring
Spring-starter-project- package war file
Converting a Spring Boot JAR Application to a WAR
Enabling Cross Origin Requests
@CrossOrigin
@RequestMapping("hello")
public Greeting sayHello(
@RequestParam(value="name", required=false, defaultValue="Stranger") String name,
HttpServletResponse response) {
//-- response body
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}