SpringCloud

@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)。get或post都可以。 

优化合并getpost

package org.gioet.plant.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Map;

public class Http {
//	public static void main(String[] args) {
//		new Http().sendPostRequest("http://localhost:9101/plantServer/getGuestManagerByArea",(Map<String, Object>) JSON.parse("{\"range\":[{\"area\":\"1\"},{\"area\":\"2\"}]}") );
//	}
	/** 发送http GET/POST请求
	 *
	 * @param httpMethod GET/POST
	 * @param url 请求地址
	 * @param param 请求体Body中json
	 * @return
	 */
	public Object sendRequest(String httpMethod,String url, Map<String, Object> param) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		//HttpEntity<String> entity = new HttpEntity<String>(headers);
		HttpEntity<Map<String, Object>> entity = new HttpEntity<>(param, headers);
		RestTemplate restTemplate = new RestTemplate();
		if(httpMethod==HttpMethod.GET.toString()) {
			//修改restTemplate的RequestFactory使其支持Get携带body参数
			restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());
			String strBody = restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
			return JSONObject.parseObject(strBody, Object.class);
		}else{
			String strBody=restTemplate.exchange(url, HttpMethod.POST, entity,String.class).getBody();
			return JSONObject.parseObject(strBody,Object.class);
		}
	}
}

class HttpComponentsClientRestfulHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {
	@Override
	protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
		if (httpMethod == HttpMethod.GET) {
			return new HttpGetRequestWithEntity(uri);
		}
		return super.createHttpUriRequest(httpMethod, uri);
	}

	static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
		public HttpGetRequestWithEntity(final URI uri) {
			super.setURI(uri);
		}
		@Override
		public String getMethod() {
			return HttpMethod.GET.name();
		}
	}
}

 

模拟一个pos请求 

模拟一个get请求 

 

应用:中转接口,相当于反向代理

@RestController
@AllArgsConstructor
@RequestMapping("plantServer")
@Api(value = "业务总接口", tags = "业务总接口")
public class PlantController extends BladeController {
	@Autowired
	WebApplicationContext applicationContext;
	@RequestMapping("*")
	@ApiOperation(value = "中转所有plantServer", notes = "中转所有plantServer")
	public R all(@RequestBody Map<String, Object> param) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();

		Logger logger= LoggerFactory.getLogger(PlantController.class);
		logger.info("获得方法"+request.getMethod() +" 获得路径"+request.getRequestURI());

		String BaseUrl="http://dev_gioetplantserver_1:3000";
		return R.data(new Http().sendRequest(request.getMethod(),BaseUrl+request.getRequestURI(),param));
	}
}

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐