java - 找不到类的序列化程序...(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)
问题描述
目标:创建一个 API,允许我在文章期刊中搜索数据,在类 Item 中检索其数据,并以 Json 格式显示。路径:我使用 Service Builder Portlet 插件在 Liferay 6.1 上创建了一个项目。在我的 service.xml 文件中,我创建了一个 Item 实体。此实体中的字段将使用恢复的文章日志数据进行更新。然后,此数据以 JSON 格式显示。
遇到的问题:我可以在 Eclipse 控制台上用 _log.info (Item) 以 JSON 格式显示数据。所以我把它们找回来,但我不能通过邮递员发布它们。
他向我展示了这个错误:
No serializer found for class java.util.Collections$3 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.beorn.tm.synchro.model.impl.ItemImpl["expandoBridge"]->$Proxy760["attributeNames"])
我的不同课程:
public class SynchroApplication extends ResourceConfig {
public SynchroApplication() {
//packages("com.beorn.tm.api");
register(BanqueRestService.class);
register(HelloWS.class);
register(JournalArticleRestService.class);
register(JacksonFeature.class);
//Register Auth filter
register(AuthenticationFilter.class);
}
public static ObjectMapper getObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.
WRITE_DATES_AS_TIMESTAMPS, true);
mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.ANY)
.withSetterVisibility(JsonAutoDetect.Visibility.ANY)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper;
}
public static String toJson(Object c) throws JsonProcessingException {
ObjectMapper mapper = getObjectMapper();
return mapper.writeValueAsString(c);
}
public static JsonNode fromJson(String input) throws Exception {
ObjectMapper mapper = getObjectMapper();
return mapper.readTree(input);
}
}
@Path("synchro")
public class JournalArticleRestService {
private static final Log _log = LogFactoryUtil.getLog(JournalArticleRestService.class);
@GET
@Path("/date")
@Produces(MediaType.APPLICATION_JSON)
public Item getJournalArticleAfter01012019() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Locale locale = Locale.FRANCE;
ItemImpl item = new ItemImpl();
try {
Date date01012019 = sdf.parse("01/01/2019");
//Companies
List<Company> companies = CompanyLocalServiceUtil.getCompanies();
for(Company company : companies) {
long companyId = company.getCompanyId();
//Groups
List<Group> groups = GroupLocalServiceUtil.getCompanyGroups(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for(Group group : groups) {
long groupId = group.getGroupId();
//Articles
List<JournalArticle> articles = JournalArticleLocalServiceUtil.getArticles(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for(JournalArticle article: articles) {
if(article.getModifiedDate().after(date01012019)) {
_log.info(article.getCreateDate() + " : " + article.getStructureId());
long articleGroupId = article.getGroupId();
long articleCompanyId = article.getCompanyId();
long userId = article.getUserId();
String userName = article.getUserName();
Date createDate = article.getCreateDate();
Date modifiedDate = article.getModifiedDate();
String className = article.getClassName();
long classPK = article.getClassPK();
int status = article.getStatus();
Date statusDate = article.getStatusDate();
item.setGroupId(articleGroupId);
item.setCompanyId(articleCompanyId);
item.setUserId(userId);
item.setUserName(userName);
item.setCreateDate(createDate);
item.setModifiedDate(modifiedDate);
item.setClassname_(className);
item.setClassPK(classPK);
item.setStatus(status);
item.setStatusDate(statusDate);
_log.info(item);
}
}
}
}
} catch (SystemException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return item;
}
}
<service-builder package-path="com.beorn.tm.synchro">
<namespace>synchronizationAPI</namespace>
<entity name="Item" uuid="true" local-service="true" remote-service="false">
<!-- PK fields -->
<column name="itemId" type="long" primary="true" />
<!-- Group instance -->
<column name="groupId" type="long" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="userId" type="long" />
<column name="userName" type="String" />
<column name="createDate" type="Date" />
<column name="modifiedDate" type="Date" />
<!-- Other fields -->
<column name="classname_" type="String" db-name="classname" />
<column name="classPK" type="long" />
<column name="status" type="int" />
<column name="statusMessage" type="String" />
<column name="statusDate" type="Date" />
<!-- Order -->
<order by="asc">
<order-column name="createDate" />
</order>
<!-- Finder methods -->
<finder name="CompanyId" return-type="Collection">
<finder-column name="companyId" />
</finder>
<finder name="GroupId" return-type="Collection">
<finder-column name="groupId" />
</finder>
<finder name="GroupIdAndClassName" return-type="Collection">
<finder-column name="groupId" />
<finder-column name="classname_" />
</finder>
<finder name="GroupIdAndClassNameAndStatus" return-type="Collection">
<finder-column name="groupId" />
<finder-column name="classname_" />
<finder-column name="status" />
</finder>
</entity>
<exceptions>
<exception>StatusNotFound</exception>
</exceptions>
</service-builder>
谢谢您的回答
解决方案
只需要添加下面的属性
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
推荐阅读
- sql - 连续查找 3 个字母
- node.js - 如何从当前意图回到对话流中的先前意图?
- android - 所有屏幕尺寸的 dimens.xml
- ssh - Docker Playbook yml - SSH 到具有不同端口的主机
- audio - 如何从 PRAAT 中的 .wav 文件中自动提取音高范围并将它们全部放入 .csv 文件中?
- oracle - ora-01858 在需要数字的地方发现了一个非数字字符 informatica
- php - Eloquent 找不到以 `datas` 结尾的表
- apache-spark - 具有自定义格式的 Apache Hudi 分区
- r - 如何在 R data.frame 中的所有行和列中查找检测单个值的重复项
- swift - [Element]' 要求 'Element' 继承自 'Object'