Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Observable as @Controller return value #4

Merged
merged 2 commits into from
Jun 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

@RestController
Expand All @@ -24,14 +26,14 @@ public AccountController(AccountService accountService) {
}

@RequestMapping(method = RequestMethod.POST)
public DeferredResult<CreateAccountResponse> createAccount(@RequestBody CreateAccountRequest request) {
return DeferredUtils.toDeferredResult(accountService.openAccount(request.getInitialBalance()).map(new Func1<EntityWithIdAndVersion<Account>, CreateAccountResponse>() {
public Observable<CreateAccountResponse> createAccount(@RequestBody CreateAccountRequest request) {
return accountService.openAccount(request.getInitialBalance()).map(new Func1<EntityWithIdAndVersion<Account>, CreateAccountResponse>() {

@Override
public CreateAccountResponse call(EntityWithIdAndVersion<Account> entityAndEventInfo) {
return new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId());
}

}));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.accounts.AccountConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({AccountConfiguration.class})
@ComponentScan
public class CommandSideWebAccountsConfiguration {
public class CommandSideWebAccountsConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.QuerySideAccountConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({QuerySideAccountConfiguration.class})
@ComponentScan
public class QuerySideWebConfiguration {
public class QuerySideWebConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

import java.math.BigDecimal;
Expand All @@ -24,13 +26,13 @@ public AccountQueryController(AccountQueryService accountInfoQueryService) {
}

@RequestMapping(value="/accounts/{accountId}", method = RequestMethod.GET)
public DeferredResult<GetAccountResponse> get(@PathVariable String accountId) {
return DeferredUtils.toDeferredResult(accountInfoQueryService.findByAccountId(new EntityIdentifier(accountId)).map(new Func1<AccountInfo, GetAccountResponse>() {
public Observable<GetAccountResponse> get(@PathVariable String accountId) {
return accountInfoQueryService.findByAccountId(new EntityIdentifier(accountId)).map(new Func1<AccountInfo, GetAccountResponse>() {
@Override
public GetAccountResponse call(AccountInfo accountInfo) {
return new GetAccountResponse(accountInfo.getId(), new BigDecimal(accountInfo.getBalance()));
}
}));
});
}

@ResponseStatus(value= HttpStatus.NOT_FOUND, reason="account not found")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.util;

import rx.Observable;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;


public class ObservableReturnValueHandler implements HandlerMethodReturnValueHandler {

@Override
public boolean supportsReturnType(MethodParameter returnType) {
return Observable.class.equals(returnType.getParameterType());
}

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws Exception {

if (returnValue == null) {
return;
}
DeferredResult<?> d = DeferredUtils.toDeferredResult((Observable<?>) returnValue);
WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(d, mavContainer);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.transactions.MoneyTransferConfiguration;
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@Import({MoneyTransferConfiguration.class})
@ComponentScan
public class CommandSideWebTransactionsConfiguration {
public class CommandSideWebTransactionsConfiguration extends WebMvcConfigurerAdapter {

@Autowired
private RequestMappingHandlerAdapter adapter;

@PostConstruct
public void init() {
// https://jira.spring.io/browse/SPR-13083
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(adapter.getReturnValueHandlers());
handlers.add(0, new ObservableReturnValueHandler());
adapter.setReturnValueHandlers(handlers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import rx.Observable;
import rx.functions.Func1;

@RestController
Expand All @@ -26,16 +28,16 @@ public MoneyTransferController(MoneyTransferService moneyTransferService) {
}

@RequestMapping(method = RequestMethod.POST)
public DeferredResult<CreateMoneyTransferResponse> createMoneyTransfer(@RequestBody CreateMoneyTransferRequest request) {
public Observable<CreateMoneyTransferResponse> createMoneyTransfer(@RequestBody CreateMoneyTransferRequest request) {
TransferDetails transferDetails = new TransferDetails(new EntityIdentifier(request.getFromAccountId()), new EntityIdentifier(request.getToAccountId()), request.getAmount());
return DeferredUtils.toDeferredResult(moneyTransferService.transferMoney(transferDetails).map(new Func1<EntityWithIdAndVersion<MoneyTransfer>, CreateMoneyTransferResponse>() {
return moneyTransferService.transferMoney(transferDetails).map(new Func1<EntityWithIdAndVersion<MoneyTransfer>, CreateMoneyTransferResponse>() {

@Override
public CreateMoneyTransferResponse call(EntityWithIdAndVersion<MoneyTransfer> entityAndEventInfo) {
return new CreateMoneyTransferResponse(entityAndEventInfo.getEntityIdentifier().getId());
}

}));
});
}

}