首页 > 解决方案 > Spring Boot 和 Swagger 文本/html 响应映射

问题描述

我有一个非常简单的 java spring boot + swagger 项目。

仅出于测试目的,我创建了两个映射类:Names.java 和 NamesContainer.java

public class Names {

@XmlAttribute(name="ref")
@ApiModelProperty(notes = "The auto-generated version of the product...")
private String key;

@XmlValue
@ApiModelProperty(notes = "The auto-generated version of the product...")
private String name;....-> rest of the class(Default constuctor and getters and setters)

............

    @XmlRootElement(name="root")
public class NamesContainer {

    @XmlElement(name="listNames")
    @ApiModelProperty(notes = "The auto-generated version of the product")
    private List<Names> listNames;....-> rest of the class(Default constuctor and getters and setters)

对于响应,我使用一种 @Get 方法:

    @RequestMapping(method = RequestMethod.GET, value = "/api/javainuse")
    @ApiOperation(value = "Get a scheduled process by id.",notes = "This is note ;)",response = NamesContainer.class,code = HttpURLConnection.HTTP_OK, produces="text/html")
    @ApiResponses(value = {@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "set in case of success. Returns the requested scheduled process",  response = NamesContainer.class)})

    public NamesContainer sayHello() {

        Map<String, String> mapNames = new HashMap<String, String>();
        mapNames.put("Name1", "Docnho");
        mapNames.put("Name2", "Silvia");
        mapNames.put("Name3", "Pepa");
        mapNames.put("Name4", "Mima");
        mapNames.put("Name5", "Mohamed");

        List<Names> listNames = new ArrayList<Names>();

    for(Map.Entry<String, String> entryName : mapNames.entrySet())
    {
        listNames.add(new Names(entryName.getKey(), entryName.getValue()));
    }

    NamesContainer container = new NamesContainer(listNames);


    return container;
}

如果我使用products="application/json"products="application/xml",结果如预期: 在此处输入图像描述

在此处输入图像描述

但是如果我尝试使用produces="text/html"

反应不如预期: 在此处输入图像描述

响应机构是;

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Fri Mar 15 18:43:55 EET 2019</div><div>There was an unexpected error (type=Not Acceptable, status=406).</div><div>Could not find acceptable representation</div></body></html> 

问题是是否有可能以生成 HTML 响应的方式映射我现有的对象 NamesContainer.java 以及如何做到这一点?

标签: javahtmlspring-bootswaggerswagger-ui

解决方案


没有办法(没有现有的方法)将 POJO 字段映射到带有注释的 html。

Instread 可以使用 Spring 提出的开箱即用的其他方式将 POJO(模型)绑定到 html:Thymleaf模板、Freemarker 模板和 JSP 页面。

以下是其中一种可能的解决方案的示例:

  1. 使用 html Thymleaf 模板创建 HTML 页面。例如一个table.html视图:

<body>
    <table>
    <tr>
        <th>Key</th>
        <th>Name</th>
    </tr>
    <tr th:each="mapEnty: ${mapNames}">
        <td th:text="${mapEnty.key}" />
        <td th:text="${mapEnty.value}" />
    </tr>
    </table>
</body>

  1. 在 Spring 中为“text/html”内容类型创建@RequestMapping @Controller,填写模型并返回“表”视图。例如:
    @GetMapping(value = "/api/javainuse", produces = MediaType.TEXT_HTML_VALUE)
    public String table(Model model) {
        Map<String, String> mapNames = new HashMap<String, String>();
        ...
        model.addAttribute("mapNames", mapNames);
        return "table";
    }

推荐阅读