相关疑难解决方法(0)

Spring RestTemplate GET参数

我必须REST拨打包含自定义标头和查询参数的电话.我HttpEntity只用标题(没有正文)设置我,我使用RestTemplate.exchange()如下方法:

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);

HttpEntity entity = new HttpEntity(headers);

HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);
Run Code Online (Sandbox Code Playgroud)

这在客户端失败,dispatcher servlet无法解析对处理程序的请求.调试它后,看起来似乎没有发送请求参数.

当我POST使用请求正文进行交换而没有查询参数时,它可以正常工作.

有没有人有任何想法?

java rest spring

227
推荐指数
9
解决办法
35万
查看次数

Spring Resttemplate异常处理

以下是代码段; 基本上,当错误代码不是200时,我试图传播异常.

ResponseEntity<Object> response = restTemplate.exchange(url.toString().replace("{version}", version),
                    HttpMethod.POST, entity, Object.class);
            if(response.getStatusCode().value()!= 200){
                logger.debug("Encountered Error while Calling API");
                throw new ApplicationException();
            }
Run Code Online (Sandbox Code Playgroud)

但是,如果来自服务器的500响应,我将获得异常

org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE]
Run Code Online (Sandbox Code Playgroud)

我真的需要在try中包装其余的模板交换方法吗?那么代码的目的是什么?

rest spring exception-handling resttemplate

88
推荐指数
8
解决办法
13万
查看次数

如何使用x-www-form-urlencoded正文发送帖子请求

邮递员要求

如何在java中,我可以发送请求x-www-form-urlencoded header.我不明白如何发送带有键值的正文,就像上面的截图一样.

我试过这段代码:

String urlParameters =
  cafedra_name+ data_to_send;
URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");

      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  

      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();
Run Code Online (Sandbox Code Playgroud)

但在回复中,我没有收到正确的数据.

java

37
推荐指数
2
解决办法
9万
查看次数

什么是restTemplate.exchange()方法?

实际上这个restTemplate.exchange()方法做了什么?

@RequestMapping(value = "/getphoto", method = RequestMethod.GET)
public void getPhoto(@RequestParam("id") Long id, HttpServletResponse response) {

    logger.debug("Retrieve photo with id: " + id);

    // Prepare acceptable media type
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    acceptableMediaTypes.add(MediaType.IMAGE_JPEG);

    // Prepare header
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(acceptableMediaTypes);
    HttpEntity<String> entity = new HttpEntity<String>(headers);

    // Send the request as GET
    ResponseEntity<byte[]> result = 
        restTemplate.exchange("http://localhost:7070/spring-rest-provider/krams/person/{id}", 
                              HttpMethod.GET, entity, byte[].class, id);

    // Display the image
    Writer.write(response, result.getBody());
}
Run Code Online (Sandbox Code Playgroud)

rest resttemplate

25
推荐指数
3
解决办法
6万
查看次数

RestTemplate uriVariables未展开

我尝试使用弹簧RestTemplate.getForObject()访问休息端点,但我的uri变量未展开,并作为参数附加到url.这是我到目前为止所得到的:

Map<String, String> uriParams = new HashMap<String, String>();
uriParams.put("method", "login");
uriParams.put("input_type", DATA_TYPE);
uriParams.put("response_type", DATA_TYPE);
uriParams.put("rest_data", rest_data.toString());
String responseString = template.getForObject(endpointUrl, String.class, uriParams);
Run Code Online (Sandbox Code Playgroud)

endpointUrl变量的值是,http://127.0.0.1/service/v4_1/rest.php并且它的确是它所谓的,但我希望http://127.0.0.1/service/v4_1/rest.php?method=login&input_type...被调用.任何提示都表示赞赏.

我正在使用Spring 3.1.4.RELEASE

问候.

java spring resttemplate

22
推荐指数
2
解决办法
2万
查看次数

Using a map to set parameters for a rest call using RestTemplate

I am currently using a piece of code to set parameters and I do a REST call to a URL using restTemplate, it works fine:

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("grant_type", grantType);
map.add("client_id", clientId);
map.add("client_secret", clientSecret);
HttpEntity<?> entity = new HttpEntity<Object>(map);
restTemplate.exchange("myurl", HttpMethod.POST, entity, Void.class);
Run Code Online (Sandbox Code Playgroud)

But if I am using a LinkedMultiValueMap it's because I looked on the web ;)

And if I replace it by a HashMap, it works as well, so can anyone tell me …

java spring spring-mvc spring-restcontroller

6
推荐指数
2
解决办法
1万
查看次数