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:
But if I try to use produces="text/html"
The response is not as expected:
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?
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:
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>
@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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With