java Springboot GET请求端点

nqwrtyyt  于 2023-05-12  发布在  Java
关注(0)|答案(1)|浏览(112)

下面我在我的服务层中附加了一个方法。它处理GET请求。下面的方法是工作,但当测试它在 Postman ,当我发送一个新的ID,在第一次发送请求时,它返回一个空数组,但在第二次发送请求时,它返回数组与保存的值在t1和t2对象。我不明白为什么在我第一次发送请求时它返回一个空数组。下面我附上了我的控制器和服务方法。

package com.websafe.ccmsbe.controller;

import com.websafe.ccmsbe.entity.CookieBannerTemplate;
import com.websafe.ccmsbe.service.CookieBannerTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "api/template")
@CrossOrigin(value = "http://localhost:3000")
public class CookieBannerTemplateController {

    @Autowired
    CookieBannerTemplateService cookieBannerTemplateService;

    @GetMapping("getAllTemplates/{websiteID}")
    public List<CookieBannerTemplate> getAllTemplates(@PathVariable(name = "websiteID")Long websiteID){
                return cookieBannerTemplateService.getAllTemplates(websiteID);
    }
}
@Service
public class CookieBannerTemplateService {

    @Autowired
    CookieBannerTemplateRepository cookieBannerTemplateRepository;

    @Autowired
    CookieBannerRepository cookieBannerRepository;

    @Autowired
    WebsiteRepository websiteRepository;

  

    public List<CookieBannerTemplate> getAllTemplates(Long websiteID) {
        Website website = websiteRepository.findById(websiteID).orElse(null);
        if (website != null){
            CookieBanner cookieBanner = website.getCookieBanner();
            if( cookieBanner == null) {
                cookieBanner = new CookieBanner();
                cookieBanner.setWebsite(website);
                cookieBanner.setBannerAlignment("Right");
                cookieBanner.setBannerColor("Dark Grey");
                cookieBanner.setBannerPosition("Center");
                cookieBannerRepository.save(cookieBanner);

                int n = website.getPrivacyRegulations().size();
                System.out.println("length = " + n);

                CookieBannerTemplate t1 = new CookieBannerTemplate();
                CookieBannerTemplate t2 = new CookieBannerTemplate();

                t1.setCookieBanner(cookieBanner);
                t1.setTemplateContent("default content");
                t1.setTemplateRegulation("GDPR");
                t1.setTemplateName("Websafe-GDPR");
                cookieBannerTemplateRepository.save(t1);

                t2.setCookieBanner(cookieBanner);
                t2.setTemplateContent("default content");
                t2.setTemplateRegulation("CCPA");
                t2.setTemplateName("Websafe-CCPA");
                cookieBannerTemplateRepository.save(t2);
                
                return cookieBanner.getCookieBannerTemplates();
            }

            return website.getCookieBanner().getCookieBannerTemplates();
        }
        return null;
    }
}

如果websiteID已经存在于cookiebanner表中,则端点工作正常。但是如果websiteID在表中不存在,第一次它返回一个空数组,第二次发送请求时它会工作并返回保存的值。

ycl3bljg

ycl3bljg1#

cookieBanner 为null时,您将创建一个新的CookieBanner,但不会将其设置为 website。在cookieBanner = new CookieBanner();之后,尝试website.setCookieBanner(cookieBanner);,看看是否适合您。

相关问题