Manuelarte Manuelarte - 1 month ago 6
JSON Question

Don't show the map entry in a JSON

I have the following endpoint:

@RestController
@RequestMapping(value = "/info")
public class InfoEndPoint {

....

@Autowired
public InfoEndPoint(....) {
....
}

@RequestMapping(method = RequestMethod.GET)
public Info get(final HttpServletRequest httpServletRequest) {
...
return info;
}

}


The info object is the following:

public class Info {

private App app;

public App getApp() { return app; }
public void setApp(final App app) { this.app = app; }
}

public class App {

@JsonUnwrapped
private Map<String, String> map;

public App() { map = new HashMap<>(); }

public Map<String, String> getMap() { return map; }
public void setMap(final Map<String, String> map) { this.map = map; }
}


When I call to the rest endpoint, the response is the following:

{
"app": {
"map": {
"description": "Unity-web",
"name": "Unity"
}
}
}


But, I would like to remove the map entry, and have this response

{
"app": {
"description": "project",
"name": "project"
}
}


What am I doing wrong?

Answer

The solution is described here Why i'm not able to unwrapp and serialize java map using jackson java library?.

Basically it involved calling to @JsonAnyGetter