TestController.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.free.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.free.config.CustomizationException;
  4. import com.free.config.ExceptionEnum;
  5. import com.free.config.ResponseFormat;
  6. import com.free.entity.Test;
  7. import com.free.mq.MqListeners;
  8. import com.free.service.TestService;
  9. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.*;
  12. import java.time.LocalDateTime;
  13. import java.time.format.DateTimeFormatter;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.UUID;
  18. @RestController
  19. @RequestMapping("/test")
  20. public class TestController {
  21. @Autowired
  22. private TestService testService;
  23. @Autowired
  24. RabbitTemplate rabbitTemplate; // 使用RabbitTemplate,这提供了接收/发送等等方法
  25. @GetMapping("/mq/send")
  26. public Object mqSendTest() {
  27. String messageId = String.valueOf(UUID.randomUUID());
  28. String messageData = "message: M A N ";
  29. String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  30. Map<String, Object> manMap = new HashMap<>();
  31. manMap.put("messageId", messageId);
  32. manMap.put("messageData", messageData);
  33. manMap.put("createTime", createTime);
  34. rabbitTemplate.convertAndSend(MqListeners.exName, MqListeners.queueName + "1", manMap);
  35. return ResponseFormat.success();
  36. }
  37. /**
  38. * 列表查询
  39. *
  40. * @return
  41. */
  42. @GetMapping()
  43. public Object list() {
  44. List list = testService.list();
  45. return ResponseFormat.success(list);
  46. }
  47. /**
  48. * 创建数据
  49. *
  50. * @param test
  51. * @return
  52. */
  53. @PostMapping()
  54. public Object save(@RequestBody Test test) {
  55. boolean result = testService.save(test);
  56. return ResponseFormat.success((result));
  57. }
  58. /**
  59. * 修改数据
  60. *
  61. * @param id
  62. * @param test
  63. * @return
  64. */
  65. @PostMapping("/{id}")
  66. public Object update(@PathVariable long id, @RequestBody Test test) {
  67. QueryWrapper<Test> qw = new QueryWrapper<Test>();
  68. qw.eq("id", id);
  69. Long num = testService.count(qw);
  70. if (num <= 0)
  71. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  72. testService.updateById(test);
  73. Test newData = testService.getById(id);
  74. return ResponseFormat.success(newData);
  75. }
  76. @DeleteMapping("/{id}")
  77. public Object delete(@PathVariable long id) {
  78. QueryWrapper<Test> qw = new QueryWrapper<Test>();
  79. qw.eq("id", id);
  80. Long num = testService.count(qw);
  81. if (num <= 0)
  82. throw new CustomizationException(ExceptionEnum.NOT_FOUND);
  83. testService.removeById(id);
  84. return ResponseFormat.success();
  85. }
  86. }