java http框架_java-Spring HTTP客户端

java-Spring HTTP客户端

我是Spring的新手,我需要Java应用程序通过HTTP(JSON,RESTful)连接到另一个API。 Spring框架是否具有JSON HTTP Rest Client之类的东西? Spring开发人员通常使用什么?

marcelorocks asked 2020-07-25T09:45:09Z

3个解决方案

44 votes

通过以下工作,我实现了所需的功能:

import org.springframework.http.HttpEntity;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpMethod;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestTemplate;

public class RestClient {

private String server = "http://localhost:3000";

private RestTemplate rest;

private HttpHeaders headers;

private HttpStatus status;

public RestClient() {

this.rest = new RestTemplate();

this.headers = new HttpHeaders();

headers.add("Content-Type", "application/json");

headers.add("Accept", "*/*");

}

public String get(String uri) {

HttpEntity requestEntity = new HttpEntity("", headers);

ResponseEntity responseEntity = rest.exchange(server + uri, HttpMethod.GET, requestEntity, String.class);

this.setStatus(responseEntity.getStatusCode());

return responseEntity.getBody();

}

public String post(String uri, String json) {

HttpEntity requestEntity = new HttpEntity(json, headers);

ResponseEntity responseEntity = rest.exchange(server + uri, HttpMethod.POST, requestEntity, String.class);

this.setStatus(responseEntity.getStatusCode());

return responseEntity.getBody();

}

public void put(String uri, String json) {

HttpEntity requestEntity = new HttpEntity(json, headers);

ResponseEntity responseEntity = rest.exchange(server + uri, HttpMethod.PUT, requestEntity, null);

this.setStatus(responseEntity.getStatusCode());

}

public void delete(String uri) {

HttpEntity requestEntity = new HttpEntity("", headers);

ResponseEntity responseEntity = rest.exchange(server + uri, HttpMethod.DELETE, requestEntity, null);

this.setStatus(responseEntity.getStatusCode());

}

public HttpStatus getStatus() {

return status;

}

public void setStatus(HttpStatus status) {

this.status = status;

}

}

marcelorocks answered 2020-07-25T09:45:23Z

16 votes

最简单的方法是使用RestTemplate,请在Spring官方博客上查看此文章:

RestTemplate是用于客户端HTTP访问的中央Spring类。

这是GET的示例:

String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class, "42", "21");

Angular University answered 2020-07-25T09:45:52Z

1 votes

我做到了以下方式:

import java.io.FileReader;

import java.util.HashMap;

import java.util.Map;

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;

import org.springframework.http.HttpEntity;

import org.springframework.http.ResponseEntity;

import org.springframework.http.converter.StringHttpMessageConverter;

import org.springframework.util.LinkedMultiValueMap;

import org.springframework.util.MultiValueMap;

import org.springframework.web.client.RestTemplate;

public class PostRequestMain {

/**

* POST with Headers call using Spring RestTemplate

*

*

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

MultiValueMap headers = new LinkedMultiValueMap();

Map map = new HashMap();

map.put("Content-Type", "application/json");

headers.setAll(map);

Map req_payload = new HashMap();

req_payload.put("name", "piyush");

HttpEntity> request = new HttpEntity<>(req_payload, headers);

String url = "http://localhost:8080/portal-name/module-name/";

// Create a new RestTemplate instance

RestTemplate restTemplate = new RestTemplate();

// Add the String message converter

restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

ResponseEntity response = restTemplate.postForEntity(url, request, String.class);

System.out.println(response);

}

private static void getPayloadMap() {

JSONParser parser = new JSONParser();

try {

Object obj = parser.parse(new FileReader("C:\\Piyush\\test.json"));

JSONObject jsonObject = (JSONObject) obj;

Map payLoadMap = new HashMap();

payLoadMap.putAll(jsonObject);

System.out.println(jsonObject);

} catch (Exception e) {

}

}

}

Piyush Mittal answered 2020-07-25T09:46:12Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值