HelloController.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package hello;
  2. import static java.util.Comparator.comparing;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.ThreadLocalRandom;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.SpringApplication;
  9. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.ModelAttribute;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import org.springframework.web.bind.annotation.ResponseBody;
  16. @Controller
  17. public final class HelloController {
  18. @Autowired
  19. JdbcTemplate jdbcTemplate;
  20. @RequestMapping("/plaintext")
  21. @ResponseBody
  22. String plaintext() {
  23. return "Hello, World!";
  24. }
  25. @RequestMapping("/json")
  26. @ResponseBody
  27. Map<String, String> json() {
  28. return Map.of("message", "Hello, World!");
  29. }
  30. @RequestMapping("/db")
  31. @ResponseBody
  32. World db() {
  33. return randomWorld();
  34. }
  35. @RequestMapping("/queries")
  36. @ResponseBody
  37. World[] queries(@RequestParam String queries) {
  38. var worlds = new World[parseQueryCount(queries)];
  39. Arrays.setAll(worlds, i -> randomWorld());
  40. return worlds;
  41. }
  42. @RequestMapping("/updates")
  43. @ResponseBody
  44. World[] updates(@RequestParam String queries) {
  45. var worlds = new World[parseQueryCount(queries)];
  46. Arrays.setAll(worlds, i -> randomWorld());
  47. for (var world : worlds) {
  48. world.randomNumber = randomWorldNumber();
  49. jdbcTemplate.update(
  50. "UPDATE world SET randomnumber = ? WHERE id = ?",
  51. world.randomNumber,
  52. world.id);
  53. }
  54. return worlds;
  55. }
  56. @RequestMapping("/fortunes")
  57. @ModelAttribute("fortunes")
  58. List<Fortune> fortunes() {
  59. var fortunes =
  60. jdbcTemplate.query(
  61. "SELECT * FROM fortune",
  62. (rs, rn) -> new Fortune(rs.getInt("id"), rs.getString("message")));
  63. fortunes.add(new Fortune(0, "Additional fortune added at request time."));
  64. fortunes.sort(comparing(fortune -> fortune.message));
  65. return fortunes;
  66. }
  67. private World randomWorld() {
  68. return jdbcTemplate.queryForObject(
  69. "SELECT * FROM world WHERE id = ?",
  70. (rs, rn) -> new World(rs.getInt("id"), rs.getInt("randomnumber")),
  71. randomWorldNumber());
  72. }
  73. private static int randomWorldNumber() {
  74. return 1 + ThreadLocalRandom.current().nextInt(10000);
  75. }
  76. private static int parseQueryCount(String textValue) {
  77. if (textValue == null) {
  78. return 1;
  79. }
  80. int parsedValue;
  81. try {
  82. parsedValue = Integer.parseInt(textValue);
  83. } catch (NumberFormatException e) {
  84. return 1;
  85. }
  86. return Math.min(500, Math.max(1, parsedValue));
  87. }
  88. public static final class Fortune {
  89. public int id;
  90. public String message;
  91. public Fortune(int id, String message) {
  92. this.id = id;
  93. this.message = message;
  94. }
  95. }
  96. public static final class World {
  97. public int id;
  98. public int randomNumber;
  99. public World(int id, int randomNumber) {
  100. this.id = id;
  101. this.randomNumber = randomNumber;
  102. }
  103. }
  104. }