I am using jackson
to create JSON
for my REST
web service.
I have a class like below -
Foo Class -
public class Foo extends Bar {
private String id;
private String name;
private List<Test> testing;
}
Bar Class -
public class Bar {
private String username;
}
Test Class -
public class Test {
private String id;
private String desc;
}
The desired JSON output that I intend to have is as follows -
{
"username" : "ABC",
"data": {
"id" : "123".
"name" : "XYZ"
}
"testing" : [
{
"id" : "test1",
"desc" : "description1"
}
]
}
I tried all possible annotations
and want to avoid creating a Wrapper
class just for creating JSON
in a certain format.
The class Foo
is populated from the database using getters and setters.
Note - I am new to jackson
答案 0 :(得分:0)
I would suggest that it is easier to create a wrapper class instead of trying any other way to hack into the problem. You can create a wrapper class to take the input, like:
public class WrapperClasss {
private String val;
public String getVal() {
return val;
}
public void setVall(String val) {
this.val = val;
}
}
Then your code can go like this:
@Path("/path")
public class SomeWebService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ResponseObject getRegister(WrapperClasss wrapperClasss) {
MainClass classs = new MainClass();
ResponseObject responseObject = classs.someMethod(wrapperClasss.getVal());
return responseObject;
}
}
That is it. The Response Object
here is just an object that will contain your output. That will be converted to JSON by Jackson.
public class ResponseObject{
private String username;
Data data;
private List<Test> testing;
//respective getter and setters
}