没有为类找到序列化器...(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)

时间:2019-05-16 11:22:41

标签: java liferay jersey-2.0 jackson2

目标:创建一个API,该API允许我在文章日志中搜索数据,在Item类中检索其数据,并以Json格式显示。 路径:我使用Service Builder Portlet插件在Liferay 6.1上创建了一个项目。在我的service.xml文件中,我创建了一个Item实体。此实体中的字段将使用恢复的商品日志数据进行更新。然后,该数据将以JSON显示。

遇到的问题:我可以使用_log.info(项目)在Eclipse控制台上以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>

谢谢您的回答

1 个答案:

答案 0 :(得分:0)

只需要添加下面的属性

objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);