Spring Boot and Swagger text/html response mapping

问题: I have got one very simple java spring boot + swagger project. Only for test purposes I've created two mapping classes: Names.java and NamesContainer.java public class...

问题:

I have got one very simple java spring boot + swagger project.

Only for test purposes I've created two mapping classes: Names.java and 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)

For response I use one @Get method:

    @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;
}

If I use produces="application/json" or produces="application/xml", the result is as expected: enter image description here

enter image description here

But if I try to use produces="text/html"

The response is not as expected: enter image description here

and Response Body is;

<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> 

The question is is it possible to map my existing object NamesContainer.java in way in which I can generate HTML response and how to do that?


回答1:

Your Pojo may need more explicit mappings/annotations to produce HTML. I assume you're looking for something like

<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>

Im not sure which annotation can help, but thats where I would start

  • 发表于 2019-03-18 23:23
  • 阅读 ( 345 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除