美文网首页
2020-07-01【工具类】

2020-07-01【工具类】

作者: skillplus | 来源:发表于2020-07-01 10:46 被阅读0次
@Component
public class JsonUtil {
    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    public JsonUtil() {
    }

    public String obj2Json(Object obj) throws JsonProcessingException {
        ObjectMapper objectMapper = this.jacksonMessageConverter.getObjectMapper();
        return objectMapper.writeValueAsString(obj);
    }

    public <O> O json2Obj(String json, Class<O> objClass) throws IOException {
        ObjectMapper objectMapper = this.jacksonMessageConverter.getObjectMapper();
        return objectMapper.readValue(json, objClass);
    }
}
   /**
    * 判断是不是数字
    *
    * @param unknownNum
    * @return
    */
   public boolean isNumeric(String unknownNum) {
       Pattern pattern = Pattern.compile("[0-9]+");
       Matcher isNum = pattern.matcher(unknownNum);
       if (!isNum.matches()) {
           return false;
       }
       return true;
   }

HTTP发送GET请求,POST请求

maven依赖

<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
      <version>3.2.7</version>
</dependency>

代码示例

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class HttpUtil {
    /**
     * 发送post请求
     *
     * @param content
     * @param URL
     * @return
     */
    public static String sendPost(String content, String URL) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(URL);
        post.setHeader("Content-Type", "application/json");
        post.addHeader("Authorization", "Basic YWRtaW46");
        String result;
        try {
            StringEntity s = new StringEntity(content, "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(s);
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                strber.append(line + "\n");
            }
            inStream.close();
            result = strber.toString();
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                System.out.println("请求服务器成功,做相应处理");
            } else {
                System.out.println("请求服务端失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    /**
     * 发送GET请求
     *
     * @return
     */
    public static String sendGet(String url) {
        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                HttpGet httpGet = new HttpGet(url);
                client = HttpClients.createDefault();
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                System.out.println(result);
                return result;
            } finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

相关文章

网友评论

      本文标题:2020-07-01【工具类】

      本文链接:https://www.haomeiwen.com/subject/dklcqktx.html