java - 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 以及如何做到这一点?
解决方案
没有办法(没有现有的方法)将 POJO 字段映射到带有注释的 html。
Instread 可以使用 Spring 提出的开箱即用的其他方式将 POJO(模型)绑定到 html:Thymleaf模板、Freemarker 模板和 JSP 页面。
以下是其中一种可能的解决方案的示例:
- 使用 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>
- 在 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";
}
推荐阅读
- vue.js - Vuetify 使用输入控件在相同高度上对齐文本
- amazon-s3 - 如何将 OpenImages 从 Sage Maker 笔记本直接下载到 S3 存储桶?
- java - Spring Data JPA - NonUniqueResultException:查询未返回唯一结果:2
- webpack - webpack 在使用 tailwibd 指令时显示不正确的源映射
- python - Flask-SQLAlchemy 分页错误:无法为具有值 ['page'] 的端点构建 url
- laravel - 如何为我的所有模型构建通用函数
- c# - 输出的时候发现currentId不是预期的10000。为什么?如何纠正它
- react-konva - 如何在 react-konva 中裁剪图像
- php - 结帐后创建一个 cron 以在特定日期暂停 Woocommerce 订阅
- javascript - 删除 firebase/firestore 中具有唯一 ID 的元素