如何在Java中使用Rest Web Service生成JSON对象

时间:2018-07-06 13:38:27

标签: java rest ejb

我正在使用Java,正在尝试创建一个Rest Web服务,该服务可以在代码中合并一组返回json对象:

@Path("/all/{profileId}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response all(@PathParam("profileId") String profileId) {
    Response profile = null;
    Response cards = null;
    Response childs= null;
    Response returnValue = null;

    try {
        ProfilJson profilJson = profilBean.getProfil(profileId);
        List<LoyaltyCardJson> listLoyaltyCardsJson = loyaltyCardBean.getcards(profileId);
        List<LoyaltyChildJson> listLoyaltyChildsJson = loyaltyCardBean.getChildd(profileId);

        if (profilJson != null) {
            profile = JsonResponse.objectToJson(profilJson);
            cards = JsonResponse.objectToJson(listLoyaltyCardsJson);    
            childs = JsonResponse.objectToJson(listLoyaltychildsJson);
        } else {
            returnValue = JsonResponse.noDataToReturn();
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);

    }
    return   returnValue;
}

我给你一个返回对象的例子:

  

profile = [{name:“ Mary”,car:“ Fiat”}];

     

cards = [{{FirstName:“ Mack”,lastName:“ jack”},{firstName:“ Steve”,lastName:“ martin”}];

     

childs = [{firstName:“ toto”,lastName:“ jack”},{firstName:“ titi”,lastName:“ martin”}];

我要做的是生成一个全局对象,可以将其放入returnValue变量中,并且该对象必须像:

  

[{“ name”:“ Bob”,“ car”:“ Ford”,   “ cards”:[{“ Name”:“ Mack”,“ Type”:“ jack”},{“ Name”:“ Steve”,“ TypeName”:“ martin”},...],   “ childs”:[{“ firstName”:“ toto”,“ lastName”:“ jack”},{“ firstName”:“ titi”,“ lastName”:“ martin”},...]   }]

P.S card和childs对象包含一个对象列表,在这个示例中,我给每个对象提供了两个对象,只是为了帮助您了解问题。

0 个答案:

没有答案