如何在Spring中将返回字符串类型的算法更改为返回列表类型?

dw1jzc5e  于 2022-10-04  发布在  Spring
关注(0)|答案(2)|浏览(167)

这段代码的作用是:当我以列表的形式给出一个长的唯一URL时,它会将它转换成一个短URL。

@Slf4j
@Service
public class ShortUrlService {
        private static final String SERVICE_URL = "http://mysite.co/example-api.php";
        private static final String SIGNATURE = "45abc12345";
        private static final String ACTION = "shorturl";
        private static final String FORMAT = "simple";
        private static final String SERVICE_CALL_FAIL = "SERVICE_CALL_FAIL";

        private Random random = new Random();

        @Autowired
        private HttpClient httpClient;

        public String shortenUrl(String url, String pCache) {
            String shortUrl = null;
            String longUrl = url;

            if (!StringUtils.isEmpty(pCache)) {
                longUrl = addCacheParameter(url, pCache);
            }

            longUrl = longUrl.replace("&", "%26");

            try {
                shortUrl = httpClient.getRequest(buildServiceUrl(longUrl));
            } catch (IOException e) {
                log.error("Service call failed: " + e.getMessage());
                shortUrl = SERVICE_CALL_FAIL;
            }

            log.info("Short URL: " + shortUrl);

            if (shortUrl.contains("-")) {
                int rand = random.nextInt(999999);
                return shortenUrl(url, String.valueOf(rand));
            }

            return shortUrl;
        }

        private String addCacheParameter(String url, String pCache) {
            if (url.contains("?")) {
                return url + "&pCache=" + pCache;
            } else {
                return url + "?pCache=" + pCache;
            }
        }

        private String buildServiceUrl(String url) {
            StringBuilder sb = new StringBuilder();

            sb.append(SERVICE_URL);
            sb.append("?signature=");
            sb.append(SIGNATURE);
            sb.append("&action=");
            sb.append(ACTION);
            sb.append("&url=");
            sb.append(url);
            sb.append("&format=");
            sb.append(FORMAT);

            log.info("Built URL: " + sb.toString());

            return sb.toString();
        }

}

我有一个类似下面的服务。此服务的返回值为List,我要将其更改为。我在几个地方遇到了问题,你能帮我修复代码吗?

@Slf4j
@Service
public class ShortUrlService {
      private static final String SERVICE_URL = "http://mysite.co/example-api.php";
      private static final String SIGNATURE = "45abc12345";
      private static final String ACTION = "shorturl";
      private static final String FORMAT = "simple";
      private static final String SERVICE_CALL_FAIL = "SERVICE_CALL_FAIL";

      private Random random = new Random();

      @Override
      public List<String> shortenUrl(List<String> url, String pCache) {
          List<String> shortUrl = null;
          List<String> longUrl = url;

          if (!StringUtils.isEmpty(pCache)) {
              longUrl = Collections.singletonList(addCacheParameter(url, pCache));
          }

        **longUrl = longUrl.replace("&", "%26");**

          RestTemplate restTemplate = new RestTemplate();

            try {
              **shortUrl = restTemplate.getForObject(buildServiceUrl(longUrl),List.class);**
            } catch (Exception e) {
                log.error("Service call failed: " + e.getMessage());
               **shortUrl =  SERVICE_CALL_FAIL;**
            }

            log.info("Short URL: " + shortUrl);

            if (shortUrl.contains("-")) {
                int rand = random.nextInt(999999);
                return shortenUrl(url, String.valueOf(rand));
            }

            return shortUrl;
        }

        private String addCacheParameter(List<String> url, String pCache) {
            if (url.contains("?")) {
                return url + "&pCache=" + pCache;
            } else {
                return url + "?pCache=" + pCache;
            }
        }

        private String buildServiceUrl(List<String> url) {
            StringBuilder sb = new StringBuilder();

            sb.append(SERVICE_URL);
            sb.append("?signature=");
            sb.append(SIGNATURE);
            sb.append("&action=");
            sb.append(ACTION);
            sb.append("&url=");
            sb.append(url);
            sb.append("&format=");
            sb.append(FORMAT);

            log.info("Built URL: " + sb.toString());

            return sb.toString();
        }

}

在这里,httpClient.getRequest抛出一个错误,因为它仍然是一项旧技术。我想改用RestTemplate。但我用对了吗?我怎么才能把替换方法写成replaceAll呢?

2wnc66cl

2wnc66cl1#

使用for循环,缩短List中的每个URL,然后将它们添加到另一个List中以返回。

List<String> shortUrls = new ArrayList<>(urls.size());
if (!StringUtils.isEmpty(pCache)) {
  longUrl = Collections.singletonList(addCacheParameter(url, pCache));
}
RestTemplate restTemplate = new RestTemplate();
for (String url: urls) {
    String longUrl = url.replace("&", "%26");
    String shortUrl = null;
    try {
        shortUrl = restTemplate.getForObject(buildServiceUrl(longUrl),List.class);
    } catch (Exception e) {
        log.error("Service call failed: " + e.getMessage());
         shortUrl = SERVICE_CALL_FAIL;
    }
    log.info("Short URL: " + shortUrl);
    if (shortUrl.contains("-")) {
        int rand = random.nextInt(999999);
        shortUrl = shortenUrl(url, String.valueOf(rand));
    }
    shortUrls.add(shortUrl);
}
return shortUrls;
h9a6wy2h

h9a6wy2h2#

@Slf4j
@Service
public class ShortUrlService {
      private static final String SERVICE_URL = "http://mysite.co/example-api.php";
      private static final String SIGNATURE = "45abc12345";
      private static final String ACTION = "shorturl";
      private static final String FORMAT = "simple";
      private static final String SERVICE_CALL_FAIL = "SERVICE_CALL_FAIL";

      private Random random = new Random();

      @Override
      public List<String> shortenUrl(List<String> urls, String pCache, List<String> shortUrls, List<String> longUrls) {

          for(String url:urls){            
          if (!StringUtils.isEmpty(pCache)) {
              longUrl = Collections.singletonList(addCacheParameter(url, pCache));
              longUrl = longUrl.replace("&", "%26");
              longUrls.add(longUrl);
          }
          RestTemplate restTemplate = new RestTemplate();    
            try {
              **shortUrl = restTemplate.getForObject(buildServiceUrl(longUrl));**
            } catch (Exception e) {
                log.error("Service call failed: " + e.getMessage());
               **shortUrl =  SERVICE_CALL_FAIL;**
            }

            log.info("Short URL: " + shortUrl);

            if (shortUrl.contains("-")) {
                int rand = random.nextInt(999999);
                return shortenUrl(url, String.valueOf(rand));
            }
            shortUrls.add(shortUrl);

           }
            return shortUrls;
        }

        private String addCacheParameter(String url, String pCache) {
            if (url.contains("?")) {
                return url + "&pCache=" + pCache;
            } else {
                return url + "?pCache=" + pCache;
            }
        }

         private String buildServiceUrl(String url) {
            StringBuilder sb = new StringBuilder();

            sb.append(SERVICE_URL);
            sb.append("?signature=");
            sb.append(SIGNATURE);
            sb.append("&action=");
            sb.append(ACTION);
            sb.append("&url=");
            sb.append(url);
            sb.append("&format=");
            sb.append(FORMAT);

            log.info("Built URL: " + sb.toString());

            return sb.toString();
        }

}

试试这个,修改了一些部件。

相关问题