1

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完美运行,没有任何错误。

4

2 回答 2

0

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

于 2019-08-05T06:47:37.733 回答
0

%25意味着hex(25)代码中有一个ASCII字符-它是%字符。

这是正确的行为,因为您在sysparm_query:“incident_stateNOT%20IN ...”中,并且该%字符被编码为%25通过 url 参数发送它。

于 2019-08-05T06:48:36.790 回答