【发布时间】:2023-03-21 18:16:01
【问题描述】:
我有一个 maven spring 项目(作为库),我将它作为依赖项添加到 spring maven 项目(Web 服务)中。 我在两个项目中都有一些属性文件。在库中有一个validationmessages.properties 文件。
我在我的模型上使用休眠验证器注释。 例如,
@NotBlank(message = "{NotBlank-entityId}")
Private String entityId;
库中的类模型,在webservice中用作请求体,这里库休眠验证消息在webservice中不起作用。 代码如下:
型号: Task.java(在库中)
public class Task {
@NotBlank(message = "{NotNull-EntityID}")
private String entityId;
public String getEntityId() {
return entityId;
}
public void setEntityId(final String entityId) {
this.entityId = entityId;
}
}
Taskvalidationmessages.properties(在库中)
NotNull-EntityID = Entity ID can not be null.
TaskManagementConfiguration.java(在库中)
@Configuration
@PropertySources({ @PropertySource("classpath:queries.properties"),
@PropertySource("classpath:Taskvalidationmessages.properties") })
@Import(DataSourceConfiguration.class)
public class TaskManagementConfiguration {
}
TaskResource.java(webservice 项目中的控制器)
@RequestMapping(value = WebserviceConstant.CREATE_TASK, method = RequestMethod.POST, produces = WebserviceConstant.APPLICATION_JSON)
public ResponseEntity<CreateTaskResponse> createTask(
@Valid @RequestBody final Task request,
@RequestHeader(value = "access-token") final String accessToken) {
return new ResponseEntity<CreateTaskResponse>(
taskService.createTask(request, receivedToken), HttpStatus.CREATED);
}
App.java(在 Web 服务项目中)
@Configuration
@SpringBootApplication
@PropertySources({ @PropertySource("classpath:user-queries.properties") })
@Import({ TaskManagementConfiguration.class })
public class App {
public static void main(final String[] args) {
SpringApplication.run(App.class, args);
}
}
每当我点击 entityId 的空值的资源 url 时。 它给出的错误如下:
{
"fieldErrors": [
{
"field": "entityId",
"code": 200,
"message": "{NotNull-EntityID}"
}
]
}
【问题讨论】:
标签: java maven spring-boot hibernate-validator properties-file