72 lines
1.7 KiB
Java
Raw Normal View History

2021-08-06 18:59:28 +08:00
package com.ruoyi.demo.controller;
2022-01-29 11:48:41 +08:00
import com.ruoyi.common.core.domain.R;
2021-08-06 18:59:28 +08:00
import com.ruoyi.common.utils.MessageUtils;
import lombok.Data;
import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
2021-08-06 18:59:28 +08:00
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
2021-08-06 18:59:28 +08:00
/**
* 测试国际化
*
* @author Lion Li
*/
@Validated
2021-08-06 18:59:28 +08:00
@RestController
@RequestMapping("/demo/i18n")
public class TestI18nController {
2022-01-11 16:58:47 +08:00
/**
* 通过code获取国际化内容
* code为 messages.properties 中的 key
* <p>
* 测试使用 user.register.success
*
* @param code 国际化code
2022-01-11 16:58:47 +08:00
*/
@GetMapping()
public R<Void> get(String code) {
2022-01-29 11:48:41 +08:00
return R.ok(MessageUtils.message(code));
2022-01-11 16:58:47 +08:00
}
/**
* Validator 校验国际化
* 不传值 分别查看异常返回
2022-01-11 16:58:47 +08:00
* <p>
* 测试使用 not.null
*/
@GetMapping("/test1")
2022-01-29 11:48:41 +08:00
public R<Void> test1(@NotBlank(message = "{not.null}") String str) {
return R.ok(str);
}
/**
* Bean 校验国际化
* 不传值 分别查看异常返回
2022-01-11 16:58:47 +08:00
* <p>
* 测试使用 not.null
*/
@GetMapping("/test2")
2022-01-29 11:48:41 +08:00
public R<TestI18nBo> test2(@Validated TestI18nBo bo) {
return R.ok(bo);
}
@Data
public static class TestI18nBo {
@NotBlank(message = "{not.null}")
private String name;
@NotNull(message = "{not.null}")
@Range(min = 0, max = 100, message = "{length.not.valid}")
private Integer age;
}
2021-08-06 18:59:28 +08:00
}