【问题标题】:How to read .properties file from library to spring maven project [duplicate]如何从库中读取.properties文件到spring maven项目[重复]
【发布时间】: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


    【解决方案1】:

    你需要org.springframework.validation.beanvalidation.LocalValidatorFactoryBean

    在您的配置中添加验证器 bean (TaskManagementConfiguration)

    @Bean(name = "messageSource")
    public MessageSource messageSource()
    {
        ReloadableResourceBundleMessageSource bean = new ReloadableResourceBundleMessageSource();
        bean.setBasename("classpath:Taskvalidationmessages");
        bean.setDefaultEncoding("UTF-8");
        return bean;
    }
    
    @Bean(name = "validator")
    public LocalValidatorFactoryBean validator()
    {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    }
    

    【讨论】:

    • 还是不行。
    • 你能更新你的完整配置吗
    猜你喜欢
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2018-07-26
    • 2021-08-21
    相关资源
    最近更新 更多