这段代码的作用是:当我以列表的形式给出一个长的唯一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呢?
2条答案
按热度按时间2wnc66cl1#
使用for循环,缩短
List
中的每个URL,然后将它们添加到另一个List
中以返回。h9a6wy2h2#
试试这个,修改了一些部件。