8. SpringOne 2gx 2014
• 5つのトラック約120セッション
• Core Spring
• Data & Integration
•Web & JavaScript
• Applied Spring
• Big Data
• http://www.slideshare.net/SpringCentral/tag/springone2gx2014
31. Jacksonの@JsonViewサポート
interface PublicView {};
class User {
@PublicView
private String username;
private String password;
// …
}
@RestController
class UserController {
@RequestMapping("/user")
@JsonView(PublicView.class)
User getUser() {
return new User("demo", "password");
}
}
32. Jacksonの@JsonViewサポート
interface PublicView {};
class User {
@PublicView
private String username;
private String password;
// …
}
@RestController
class UserController {
@RequestMapping("/user")
@JsonView(PublicView.class)
User getUser() {
{"username":"demo"}
return new User("demo", "password");
}
}
33. JSONPサポート
@ControllerAdvice
public class JsonpAdvice extends
AbstractJsonpResponseBodyAdvice {
public JsonpAdvice() {
super("callback");
}
}
34. JSONPサポート
@RestController
class UserController {
@RequestMapping("/user")
User getUser() {
return new User("demo", "password");
}
}
GET /user?callback=foo
foo({"username":"demo",
"password":"password"})
37. 使い方
従来のXMLの場合
<mvc:annotation-driven>
<mvc:message-converters>
<bean class=“….GsonHttpMessageConverter” />
</mvc:message-converters>
</mvc:annotation-driven>
Spring Bootの場合
@Configuration
class AppConfig {
@Bean
HttpMessageConverter gsonHttpMessageConverter() {
return new GsonHttpMessageConverter();
}
}
38. ResponseEntityビルダーサポート
Before
public ResponseEntity<String> handle() {
String body = "Hi!";
HttpHeaders headers = new HttpHeaders();
headers.setLocation(location);
return new ResponseEntity(body, headers,
HttpStatus.CREATED);
}
After
public ResponseEntity<String> handle() {
URI location = …;
return ResponseEntity.created(location).body("Hi!");
}
39. RequestEntityビルダーサポート
Before
HttpHeaders headers = new HttpHeaders();
headers.setAccept(MediaType.APPLICATION_JSON);
HttpEntity entity = new HttpEntity("Hi!", headers);
restTemplate.exchange(uri, HttpMethod.POST, entity,
String.class);
After
restTemplate.exchange(RequestEntity.post(uri)
.accept(MediaType.APPLICATION_JSON)
.body(Hi!), String.class);
40. EL関数 s:mvcUrl 追加
@Controller
@RequestMapping("/hotels/{hotel}")
public class BookingController {
@RequestMapping("/bookings/{booking}")
String getBooking(@PathVariable Long booking) {/*…*/}
}
Before
<a href="${pageContext.request.contextPath}/hotels/
21/42" />
After
<a href="${s:mvcUrl('BC#getBooking').arg(0,
42).buildAndExpand(21)}" />
41. URI逆引き
static import MvcUriComponentsBuilder.*;
UriComponents uriComponents = fromMethodCall(
on(BookingController.class).getBooking(21)
).buildAndExpand(42);
URI uri = uriComponents.encode().toUri();
OR
UriComponents uriComponents =
fromMappingName("BC#getBooking")
.arg(0, 21)
.buildAndExpand(42);
URI uri = uriComponents.encode().toUri();
57. Springのキャッシュサポート
Before (Spring 3.1~)
org.springframework.cache.annotation.*
public class BookService {
@Cacheable("books")
public Book findById(String id) {/**/}
@Cacheable(value="books",key=
"T(com.example.BookIdResolver).resolve(#isbn)")
public Book findByIsbn(ISBN isbn) {/**/}
@CachePut(value="books",key="#book.id")
public void update(Book book) {/**/}
@CacheEvict("books")
public void delete(String id) {/**/}
}
58. Springのキャッシュサポート
Before (Spring 4.1の改善)
@CacheConfig("books")
public class BookService {
@Cacheable
public Book findById(String id) {/**/}
@Cacheable(key="T(com.example.BookIdResolver).resolve(#isbn)")
public Book findByIsbn(ISBN isbn) {/**/}
@CachePut(key="#book.id")
public void update(Book book) {/**/}
@CacheEvict
public void delete(String id) {/**/}
}
org.springframework.cache.annotation.*
59. JCacheサポート
After
javax.cache.annotation.*
@CacheDefaults(cacheName = "books")
public class BookService {
@CacheResult
public Book findById(String id) {/**/}
@CacheResult(cacheKeyGenerator = IsbnCacheKeyGenerator.class)
public Book findByIsbn(ISBN isbn) {/**/}
@CachePut
public void update(String id,
@CacheValue Book book) {/**/}
@CacheRemove
public void delete(String id) {/**/}}
60. Configuration
@Configuration
@EnableCaching
public class AppConfig {
@Value("classpath:my-ehcache.xml")
Resource ehCacheConfig;
@Bean
CacheManager cacheManager() {
return new EhCacheManager(EhCacheManagerUtils
.buildCacheManager(ehCacheConfig));
}
// …
}
Before
61. Configuration
Before
@Configuration
@EnableCaching
public class AppConfig {
@Bean
CacheManager cacheManager() {
return new JCacheManager();
}
// …
}
62. Configuration
Before
@Configuration
@EnableCaching
public class AppConfig {
@Bean
CacheManager cacheManager() {
javax.cache.CacheManager
cacheManager = Caching.getCachingProvider()
.getCacheManager();
// JCache standard configuration …
return new JCacheManager(cacheManager);
}
}