Spring Boot和Swagger文本/ html响应映射

Arm*_* B. 8 html java swagger swagger-ui spring-boot

我有一个非常简单的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)
Run Code Online (Sandbox Code Playgroud)

...........

    @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)
Run Code Online (Sandbox Code Playgroud)

对于响应,我使用一种@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;
}
Run Code Online (Sandbox Code Playgroud)

如果我使用produces =“ application / json”produces =“ 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> 
Run Code Online (Sandbox Code Playgroud)

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

Arm*_* B. 0

我找到了解决这个问题的几个机会,但我认为这两个是最好的:

第一个对于面向码头服务器的应用程序有意义。这是解释 -这里Produce="text/html, ..., ..."中的主要内容是MessageBodyWriter接口。如果你可以定制它,你就可以用它做everithink。

第二个也是我的最终解决方案只是为我的 .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;
    }
}
Run Code Online (Sandbox Code Playgroud)