HiAll,
i have the following problem in development branch: If i use the jersey REST service to serialize the POJO containig a list of the nested other POJO, then this nested POJO will be NOT serialized. This problem is reproducible only in branch.
If i prototype using POJO structur liked in branch, i have no problem.
The Details:
The POJO (Domain):
public class Article {
private int id;
private int name;
// getter & setter
}
The POJO ( DTO)
public class DTO {
private List<Article> articles;
private String message;
public DTO {
articles= new ArrayList<>();
}
getArticles() {
return articles;
}
getArticlesCount() {
return articles.size();
}
public void getMessage() {
return message;
}
public String getMessage() {
return message;
}
....
}
It will be created the 1 article with id = 1 and name "firstArticle" the The results of serialization after service call to find all articles:
in branch
*{"message":"1 article found","articlesCount":1
}*
in prototype
{
{"message":"1 article found","articlesCount":1[{"id":1,"name":"firstArticle"]}
}
i have no idia what's happened. I checked out all settings (web.xml, jersey version, etc.)
If you use Jackson JSON library , your POJO will be automatically handled by Jackson JSON lib to convert between JSON and POJO. You also need to add the following lines in your web.xml:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
Then in your Jersey resource class, use @Produces
and @Consumes
annotation to indicate the data format is JSON:
@Path("/myResource")
@Produces("application/json")
public class SomeResource {
@GET
public String doGetAsJson() {
...
}
}
Follow the above answer to set up Jersey POJO mappping, and then try the following nested POJO example.
Article class:
public class Article {
private int id;
private String name;
public Article(int id, String name) {
this.id = id;
this.name = name;
}
//getter and setter ...
}
The Book class that contains the Article class:
public class Book {
private List<Article> articles;
private String message;
private int articleCount;
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
setArticleCount(articles.size());
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getArticleCount() {
return articleCount;
}
public void setArticleCount(int articleCount) {
this.articleCount = articleCount;
}
}
Finally in the Jersey resource class NestedPojoResource:
@Path("/nested-pojo")
public class NestedPojoResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Book getNestedPojo() {
Book book = new Book();
List<Article> articles = new ArrayList<Article>();
articles.add(new Article(1, "firstArticle"));
articles.add(new Article(2, "secondArticle"));
book.setArticles(articles);
book.setMessage(book.getArticleCount() + " articles found");
return book;
}
}
When you go to http://example.com/myappname/nested-pojo , you will see the correct JSON output which contains the nested POJO fields:
{"articles":[{"id":1,"name":"firstArticle"},{"id":2,"name":"secondArticle"}],"message":"2 articles found","articleCount":2}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.