반응형
Recent Posts
Recent Comments
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Real Vectorism. 훨씬 더 입체적으로...

와 난관봉착했다... 본문

Java (based by Spring Boot 2)

와 난관봉착했다...

grast 2020. 9. 15. 01:05
반응형

스프링부트로 웹플럭스를 연습하던 도중

 

class UserDetails {
    private int idx;
    private String username;
    private String password;
    
    public UserDetails() {}
    public UserDetails(int idx) {
        this.idx = idx;
    }
    
    public int getIdx() { return this.idx; }
    public String getUsername() { return this.username; }
    public String getPassword() { return this.password; }
    
    public UserDetails setIdx(int idx) { this.idx = idx; return this; }
    public UserDetails setUsername(String username) { this.username = username; return this; }
    public UserDetails setPassword(String password) { this.password = password; return this; }
    
    toString은 타이핑 구찮아서 스킵
}

의 형태로 유저 정보를 담는 클래스를 작성하고

 

public interface UserDetailsService {
    UserDetails select(UserDetails item) throws Exception;
}

위는 인터페이스, 아래는 인터페이스 구현체

public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserDetailsDAO userDetailsDao;
    
    public UserDetails select(UserDetails item) throws Exception {
        return Optional.of(item)
                       .map(item -> item.getIdx())
                       .map(userDetailsDao::findById)
                       .orElse(null)
                       .get();
    }
}

이렇게 작성을 했더니

 

@RestController
public class UserDetailsController {
    private final static Logger logger = LoggerFactory.getClass(UserDetailsController.class);

    @Autowired
    private UserDetailsService userDetailsService;
    
    @RequestMapping("/user/me/{idx}")
    public Mono<String> me(ServerHttpRequest request, ServerHttpResponse response, 
            @RequestParam Map<String, String> param, 
            @PathVariable("idx") int idx) throws Exception {
        return Mono.just(
            Optional.of(idx)
                    .map(UserDetails::new)
                    .map(userDetailsService::select)
                    .orElse(null)
                    .get()
        );
    }
}

Optional 안에서 throw 체크가 필수로 걸리는 userDetailsService::select가 try ~ catch로 핸들링 할 방법이 없다......

저거 람다식 풀고 일일이 익명 인터페이스 선언식으로 써야하는건가......

반응형
Comments