我写了一个微服务,将http call
转换为API
,然后通过Angular 7获取数据。代码如下所示。
一米二米一x
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();
}
}
一米三米一x
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);
}
}
当我在tomcat中以Spring boot app
的形式运行时,incident_stateNOT%20IN6%2C7%5Eassignment_group%3D4122c7f8f09cc1002283ac3a043ae3e6
被修改为incident_stateNOT%2520IN6%252C7%255Eassignment_group%253D4122c7f8f09cc1002283ac3a043ae3e6
。由于这种不必要的修改,我无法连接到我的API。我无法找出导致这种修改的原因。是什么导致这种修改?
P.S:mvn clean install
运行完美,没有任何错误。
2条答案
按热度按时间xytpbqjk1#
URL被你正在使用的一个库进行了url编码,
sysparm_query
已经进行了url编码,所以你得到了%
更改了%25
等等。所以你可能想把sysparm_query
更改为不进行url编码。mi7gmzs62#
%25
意味着有一个hex(25)
代码的ASCII字符-它是%
字符。这是正确的行为,因为在
sysparm_query
中:“incident_stateNOT%20IN...",并且%
字符被编码为%25
以通过url参数发送它。