Last active
December 23, 2015 15:19
-
-
Save jbrisbin/6655092 to your computer and use it in GitHub Desktop.
Example usage of reactor-data with Spring Boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class TradeController { | |
@Autowired | |
private ClientComposableRepository clients; | |
@RequestMapping(value = "/{clientId}", method = RequestMethod.POST) | |
@ResponseBody | |
public DeferredResult<String> trade(@PathVariable Long clientId) { | |
final DeferredResult<String> d = new DeferredResult<String>(); | |
clients.save(clients.findOne(clientId) | |
.map(new Function<Client, Client>() { | |
public Client apply(Client cl) { | |
return cl.setTradeCount(cl.getTradeCount() + 1); | |
} | |
})) | |
.map(new Function<Client, String>() { | |
public String apply(Client cl) { | |
return "Hello " + cl.getName() + "! You now have " + cl.getTradeCount() + " trades."; | |
} | |
}) | |
.consume(new Consumer<String>() { | |
public void accept(String s) { | |
d.setResult(s); | |
} | |
}); | |
return d; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class TradeController { | |
@Autowired | |
private ClientComposableRepository clients; | |
@RequestMapping(value = "/{clientId}", method = RequestMethod.POST) | |
@ResponseBody | |
public DeferredResult<String> trade(@PathVariable Long clientId) { | |
final DeferredResult<String> d = new DeferredResult<>(); | |
clients.save(clients.findOne(clientId) | |
.map(cl -> cl.setTradeCount(cl.getTradeCount() + 1))) | |
.map(cl -> "Hello " + cl.getName() + "! You now have " + cl.getTradeCount() + " trades.") | |
.consume(d::setResult); | |
return d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Indent issue? Also may be use constructor injection to minimise mutability easier to test too. :-)
How about a Scala sample? :-)