我有一个非常简单的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;
}
如果我使用produce =“application / json”或者generate =“application / xml”,结果如预期:
但如果我尝试使用produce =“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>
问题是是否可以映射我现有的对象NamesContainer.java,我可以在其中生成HTML响应以及如何做到这一点?
没有办法(没有现成的方法)将POJO字段映射到带注释的html。
Instread可以使用Spring提出的其他方法将POJO(模型)绑定到html:Thymleaf temlates,Freemarker templates和JSP页面。
以下是一个可能的解决方案示例:
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>
@Controller
中为'text / html'内容类型创建@RequestMapping,填入模型并返回'table'视图。例如: @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";
}
TLDR:是的,这是可能的。为HTML创建Jackson数据格式模块。
我相信Spring Boot使用Jackson进行数据输出,Jackson支持这种格式:
还有更多(https://github.com/FasterXML/jackson),但没有支持HTML的格式(这怎么可能?)。
HTML呈现需要模板化。阅读Spring MVC https://spring.io/guides/gs/serving-web-content/。
请阅读有关内容谈判(https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc)的内容
您可以使用RESTful @ResponseBody方法和HTTP消息转换器,通常返回JSON或XML等数据格式。
(...)如果您愿意,视图完全能够生成JSON和XML,视图通常用于生成传统Web应用程序的HTML等表示格式。
我找到了解决这个问题的几个机会,但我认为这两个是最好的:
第一个对于面向Jetty服务器的应用程序是有意义的。这是解释 - here。 produce =“text / html,...,...”中的主要内容是MessageBodyWriter界面。如果您可以自定义它,您可以随时随地使用它。
第二个,我的最终解决方案是为我的.xml文件创建.xsl文件。我将.xml文件转换为HTML,然后完成响应。
**如果有人想在一个方法中完成所有操作,可以使用:
@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" /*add produces->xml*/)
@ApiResponses(value = {@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "set in case of success. Returns the requested scheduled process", response = NamesContainer.class)})
public Response sayHello(HttpServletResponse response) {
switch (request.getHeader("accept"))
{
case MediaType.APPLICATION_XML:
response = Response.ok().entity(/*yourEntity here (for me it was NamesContainer)*/).type(MediaType.APPLICATION_XML).build();
break;
case MediaType.TEXT_HTML:
response = Response.ok().entity(/*Transform xml to HTML with xsl and return it here as String*/).type(MediaType.TEXT_PLAIN).build();
break;
}
}
您的Pojo可能需要更明确的映射/注释来生成HTML。我假设你正在寻找类似的东西
<table>
<tr>
<th>Name1</th>
<th>Name2</th>
<th>Name3</th>
<th>Name4</th>
</tr>
<tr>
<td>Peppa</td>
<td>Mima</td>
<td>Mohamed</td>
<td>Docnho</td>
</tr>
</table>
我不确定哪个注释可以帮助,但那就是我要开始的地方