gpt4 book ai didi

java - 响应未按照我们的要求正确进行

转载 作者:行者123 更新时间:2023-12-02 08:42:43 25 4
gpt4 key购买 nike

我正在使用 Spring Boot 和 java Restful API 中的安全性。我想要根据我的要求得到适当的答复。我将向您展示我的数据库表并要求回复。我还将向您展示我尝试过的代码。我已经尝试过,但响应没有按照我的要求得到正确的结果。有人知道吗?如何做到这一点?

  1. 数据库表 This is my Event Type table

  2. 我想要上表中的回复

"eventColorLegends": [
{
"color": "Red",
"items": [
"Musician1",
"Workshop",
"Seminart"
]
},
{
"color": "Black",
"items": [
"Product Launches",
"Incentive Trips"
]
},
{
"color": "Green",
"items": [
"Dancing",
"Rotary - Service Club",
"Historical/Remembrance"
]
},
{
"color": "Yellow",
"items": [
"Party",
"Incubator - Accelerator",
"Leads - Referral"
]
},
{
"color": "Orange",
"items": [
"Charity",
"Non-profit",
"Rotary - Service Club",
"Historical/Remembrance",

]
},
{
"color": "Coffee",
"items": [
"Incubator - Accelerator"
]
},
{
"color": "Blue",
"items": [
"Leads - Referral",
"Historical/Remembrance",
"Rotary - Service Club",
"For-Profit Company",

]
},
]
  • 我在 serviceImpl 类中尝试了代码
  • public GenericResponse findAllTypeOfEvents() {
    try {
    List<TypesOfEvents> typesOfEventsList = typeOfEventsDao.findAll();
    List<String> listEvents = new ArrayList<String>();
    for (int i = 0; i < typesOfEventsList.size(); i++) {
    for (int j = i + 1; j < typesOfEventsList.size()- 1; j++) {
    if (typesOfEventsList.get(i).getColor().equalsIgnoreCase(typesOfEventsList.get(j).getColor())) {
    if (typesOfEventsList.get(j).getType().equalsIgnoreCase(typesOfEventsList.get(j).getType())){
    listEvents.add(i,typesOfEventsList.get(j).getType());
    System.out.println(typesOfEventsList.get(i).getColor() + " : " + typesOfEventsList.get(j).getType());
    }
    }
    }
    }
    return APIResponseBuilder.build(true, listEvents.toArray(), typesOfEventsList.size() > 0 ? commonMessages.findAllTrue : commonMessages.findAllFalse);
    } catch (Exception e) {
    logger.error(commonMessages.findAllException + "TypesOfEvents " + e.getMessage());
    return APIResponseBuilder.build(false, e.getMessage(), commonMessages.exception);
    }
    }

    4.我的回应

    "data": [
    "Seminart",
    "For-Profit Company",
    "For-Profit Company",
    "Historical/Remembrance",
    "Leads - Referral",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "For-Profit Company",
    "For-Profit Company",
    "For-Profit Company",
    "Historical/Remembrance",
    "Seminart",
    "Seminart",
    "Historical/Remembrance",
    "Workshop",
    "Leads - Referral",
    "Leads - Referral",
    "For-Profit Company",
    "Incubator - Accelerator",
    "Seminart",
    "Historical/Remembrance",
    "Leads - Referral",
    "Leads - Referral",
    "Leads - Referral",
    "Incubator - Accelerator",
    "For-Profit Company",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Leads - Referral",
    "Workshop",
    "Seminart",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Non-profit",
    "Incubator - Accelerator",
    "Historical/Remembrance",
    "Rotary - Service Club",
    "Leads - Referral",
    "Incubator - Accelerator",
    "Rotary - Service Club",
    "Historical/Remembrance",
    "Historical/Remembrance",
    "Incentive Trips",
    "Workshop",
    "Seminart",
    "Workshop"
    ]
    }

    最佳答案

    创建 Map<String, List<String>>映射每种颜色的类型列表。

    Map<String, List<String>> map = new HashMap<>();
    for (int i = 0; i < typesOfEventsList.size(); i++) {
    if (fruits.containsKey(typesOfEventsList.get(i).getColor())){
    List<String> list = map.get(typesOfEventsList.get(i).getColor());
    list.add(typesOfEventsList.get(i).getType());
    map.put(typesOfEventsList.get(i).getColor(), list);
    } else {
    List<String> list = new ArrayList<String>();
    list.add(typesOfEventsList.get(i).getType());
    map.put(typesOfEventsList.get(i).getColor(), list);
    }
    }

    然后创建类TypeByColor

    class TypeByColor {
    String color;
    List<String> items;
    }

    然后从 map 创建列表

        List<TypeByColor> data = new ArrayList<>();
    for (String name : map.keySet())
    {
    list.add(new TypeByColor(name,map.get(name)));
    }

    关于java - 响应未按照我们的要求正确进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61273479/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com