http call
我编写了一个微服务API
,然后通过 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 中将其作为 a 运行时,incident_stateNOT%20IN6%2C7%5Eassignment_group%3D4122c7f8f09cc1002283ac3a043ae3e6
被修改为incident_stateNOT%2520IN6%252C7%255Eassignment_group%253D4122c7f8f09cc1002283ac3a043ae3e6
. 由于这种不必要的修改,我无法连接到我的 API。我无法弄清楚是什么导致了这种修改。是什么导致了这种修改?
PS:mvn clean install
完美运行,没有任何错误。