道路环境监测node后端和java后台代码
wangrong
2021-11-24 ac1a5f4542431c28b5865acbeb81985cefdbc2a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.demo.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
 
public class RestTemplateUtil {
 
    private static Logger logger = LoggerFactory.getLogger(RestTemplateUtil.class);
 
    public static JSONObject postDataByRestTemplate(String url, Object params, RestTemplate restTemplate){
        try {
            logger.info("request for:"+url);
            ResponseEntity<String> entity = restTemplate.postForEntity(url, JSONObject.toJSON(params), String.class);
            String body = entity.getBody();
            if (!body.startsWith("{")) {
                logger.info("response body is not start with '{' ***********************************************");
                body = "{\"data\":"+body+"}";
//                Feature feature = Feature.IgnoreNotMatch;
                return JSONObject.parseObject(body,Feature.IgnoreNotMatch);
            } else {
                return JSONObject.parseObject(body,Feature.IgnoreNotMatch);
            }
        } catch (final HttpClientErrorException httpClientErrorException) {
            logger.info("-------------------------------------------------------------------------------3\r\n"+url);
            httpClientErrorException.printStackTrace();
        } catch (HttpServerErrorException httpServerErrorException) {
            logger.info("-------------------------------------------------------------------------------2\r\n"+url);
            httpServerErrorException.printStackTrace();
        } catch (Exception exception) {
            logger.info("-------------------------------------------------------------------------------1");
            exception.printStackTrace();
        }
        return null;
 
 
    }
 
    public static JSONObject getDataByRestTemplate(String url, RestTemplate restTemplate){
        try {
            logger.info("request for:"+url);
            ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
            String body = entity.getBody();
            if (!body.startsWith("{")) {
                logger.info("response body is not start with '{' ***********************************************");
                body = "{\"data\":"+body+"}";
                return JSONObject.parseObject(body);
            } else {
                return JSONObject.parseObject(body);
            }
        } catch (final HttpClientErrorException httpClientErrorException) {
            logger.info("-------------------------------------------------------------------------------3\r\n"+url);
            httpClientErrorException.printStackTrace();
        } catch (HttpServerErrorException httpServerErrorException) {
            logger.info("-------------------------------------------------------------------------------2\r\n"+url);
            httpServerErrorException.printStackTrace();
        } catch (Exception exception) {
            logger.info("-------------------------------------------------------------------------------1");
            exception.printStackTrace();
        }
        return null;
    }
 
}