首页 > 解决方案 > 我想从 MessageResource 即 message.properties 读取我的界面内的文档部分

问题描述

我的项目结构如下

src
   - /com/dev/api
        APIInterface.java
        APIController.java
   - /resources
        -i18
           -messages.properties

我的 API 接口看起来像 APIInterface.java


import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.Size;

import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;


@RestController
@Validated
@Tag(name = "Definition Inquiry", description = "The Definition API")
public interface APIInterface {

    @Operation(summary = "{summary.api}", description = "Get List of Definitions on dp number search.")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "Successful response", content = {
                    @Content(mediaType = "application/json", schema = @Schema(implementation = DefinitionOne.class)) }),
            @ApiResponse(responseCode = "204", description = "No Content", content = {
                    @Content(mediaType = "application/json", schema = @Schema(implementation = String.class)) }),                       
            @ApiResponse(responseCode = "500", description = "Internal Error", content = {
                    @Content(mediaType = "application/json", schema = @Schema(implementation = FailureDefinition.class)) }) })
    @GetMapping("/dev/defs")
    ResponseEntity<List<DefSummary>> getDefinitions( 
            @Parameter(description = "Definition Parameter")
            @RequestParam("df") 
            @Valid
            @Size(min=7,message="{dp.length.fail.msg}")
            String df) throws Exception;
    
}

消息属性

summary.api=Get List Of Definitions API
dp.length.fail.msg=Minimum length should be {min}

当我运行应用程序并检查时,swagger 不理解要从messages.properties 替换的令牌。 在此处输入图像描述

虽然它适用于验证消息。对于dp.length.fail.msg少于 7 个字符的输入,从 messages.properties 中提供准确信息,如下图所示。 在此处输入图像描述

但是在 swagger-ui 的情况下summary.api是不可读的。

是否可以在界面中读取 messages.properties 键?

标签: javaspring-bootswagger

解决方案


推荐阅读