gpt4 book ai didi

java - URL 参数正在修改

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:57 27 4
gpt4 key购买 nike

我编写了一个微服务来制作 http callAPI然后通过Angular 7获取数据。代码如下。
<强> Connector Application

package com.ajay.dashboard.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class DellDashboardConnectorApplication {

public static void main(String[] args) {
SpringApplication.run(DellDashboardConnectorApplication.class, args);
}

@Bean
public RestTemplate getRestTemplate() {

return new RestTemplate();
}
}

<强> Connector Controller

package com.ajay.dashboard.service.controller;

import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

/*
* Created by Kulkaa
*/

@RestController
public class DellDashboardController {

private static final Logger logger = LoggerFactory.getLogger(DellDashboardController.class);

@Autowired
RestTemplate restTemplate;

@RequestMapping(method = RequestMethod.GET, value = "/incident", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> retrieveAllCircles(HttpServletRequest request) {
logger.info("DellDashboardController -> retrieveAllIncidents : invoked.");

String formUrl = "https://<api>/api/now/table/incident";

final String sysparm_query = "incident_stateNOT%20IN6%2C7%5Eassignment_group%3D4122c7f8f09cc1002283ac3a043ae3e6";
final String sysparm_display_value = "true";
final String sysparm_exclude_reference_link = "true";
try {
Map<String, String> params = new HashMap<String, String>();
URI actualUrl = UriComponentsBuilder.fromUriString(formUrl).buildAndExpand(params).toUri();
actualUrl = UriComponentsBuilder.fromUri(actualUrl).queryParam("sysparm_query", sysparm_query)
.queryParam("sysparm_display_value", sysparm_display_value)
.queryParam("sysparm_exclude_reference_link", sysparm_exclude_reference_link)
.build().toUri();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "<authorization code>");
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange(actualUrl, HttpMethod.GET, entity, String.class);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return retrieveAllCircles(request);

}

}

当我将其作为 Spring boot app 运行时在 tomcat 中,incident_stateNOT%20IN6%2C7%5Eassignment_group%3D4122c7f8f09cc1002283ac3a043ae3e6修改为 incident_stateNOT%2520IN6%252C7%255Eassignment_group%253D4122c7f8f09cc1002283ac3a043ae3e6 。由于这种不需要的修改,我无法连接到我的 API。我无法弄清楚是什么导致了这种修改。是什么导致了这种修改?

附:mvn clean install运行完美,没有任何错误。

最佳答案

URL 正在由您正在使用的库之一进行 url 编码,sysparm_query 已进行 url 编码,因此您将 % 更改为 %25 等等。因此,您可能希望将 sysparm_query 更改为不进行 url 编码。

关于java - URL 参数正在修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353331/

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