Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 231
Armer B. Avatar asked Mar 15 '19 17:03

Armer B.


1 Answers

There is no way (no already existing way) to map POJO fields to html with annotations.

Instread one can bind POJOs (model) to html using other means Spring proposes out of the box: Thymleaf temlates, Freemarker templates and JSP pages.

Here is an example of one of the possible solutions:

  1. Create HTML page using html Thymleaf template. For example a table.html view:

<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. Create a @RequestMapping for 'text/html' Content type in a Spring @Controller, fill in the Model and return the 'table' view. For example:
    @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";
    }
like image 164
da-sha1 Avatar answered Nov 10 '22 03:11

da-sha1