TestI18nController.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.ruoyi.demo.controller;
  2. import com.ruoyi.common.core.domain.R;
  3. import com.ruoyi.common.utils.MessageUtils;
  4. import lombok.Data;
  5. import org.hibernate.validator.constraints.Range;
  6. import org.springframework.validation.annotation.Validated;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import javax.validation.constraints.NotBlank;
  11. import javax.validation.constraints.NotNull;
  12. /**
  13. * 测试国际化
  14. *
  15. * @author Lion Li
  16. */
  17. @Validated
  18. @RestController
  19. @RequestMapping("/demo/i18n")
  20. public class TestI18nController {
  21. /**
  22. * 通过code获取国际化内容
  23. * code为 messages.properties 中的 key
  24. * <p>
  25. * 测试使用 user.register.success
  26. *
  27. * @param code 国际化code
  28. */
  29. @GetMapping()
  30. public R<Void> get(String code) {
  31. return R.ok(MessageUtils.message(code));
  32. }
  33. /**
  34. * Validator 校验国际化
  35. * 不传值 分别查看异常返回
  36. * <p>
  37. * 测试使用 not.null
  38. */
  39. @GetMapping("/test1")
  40. public R<Void> test1(@NotBlank(message = "{not.null}") String str) {
  41. return R.ok(str);
  42. }
  43. /**
  44. * Bean 校验国际化
  45. * 不传值 分别查看异常返回
  46. * <p>
  47. * 测试使用 not.null
  48. */
  49. @GetMapping("/test2")
  50. public R<TestI18nBo> test2(@Validated TestI18nBo bo) {
  51. return R.ok(bo);
  52. }
  53. @Data
  54. public static class TestI18nBo {
  55. @NotBlank(message = "{not.null}")
  56. private String name;
  57. @NotNull(message = "{not.null}")
  58. @Range(min = 0, max = 100, message = "{length.not.valid}")
  59. private Integer age;
  60. }
  61. }