Skip to main content

Posts

Showing posts from January, 2019

Spring5 + Rest + Agile (5)

Provide Non-blocking REST API: The benefit of non-blocking API will benefit a lot for big scale concurrency calls. We will add @EnableAsync to ResourceServerConfig @Configuration @EnableAsync @EnableResourceServer /*@EnableResourceServer enables a Spring Security filter that authenticates requests using an incoming OAuth2 token.*/ public class ResourceServerConfig extends ResourceServerConfigurerAdapter { and add new async method to demoController: @GetMapping ( "/async-hello" ) public DeferredResult<ResponseEntity<?>> helloAsync () { DeferredResult<ResponseEntity<?>> output = new DeferredResult<>() ; ForkJoinPool. commonPool ().submit(() -> { try { Thread. sleep ( 6000 ) ; } catch (InterruptedException e) { } output .setResult(ResponseEntity. ok ( HELLOWORLD )) ; }) ; return output ; } add refactor test case to support async call with mvcmock @Test public void c

Spring5 + Rest + Agile (4)

Do more with test: on the previous practice, we run the embed tomcat to test rest, and we don't need do that since spring provide mock mvc to simplify our test work. the older code: /** * The test case used general resttemplate to call api and compare the response. * the whole test is running with an actual tomcat server. */ package org.lz.boilerplate.springrest ; import org.junit.Assert ; import org.junit. Test ; import org.junit.runner. RunWith ; import org.springframework.beans.factory.annotation. Autowired ; import org.springframework.boot.test.context. SpringBootTest ; import org.springframework.boot.test.web.client.TestRestTemplate ; import org.springframework.boot.web.server. LocalServerPort ; import org.springframework.http.* ; import org.springframework.security.oauth2.client.OAuth2RestTemplate ; import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordResourceDetails ; import org.springframework.test.context.junit4.SpringRunner