This article shares the specific code of SpringBoot Http remote call for everyone for your reference. The specific content is as follows
1. When implementing remote calls, you can use feign and http remote calls. The relationship between the two is as follows:
Feign and http. Sometimes when calling third-party APIs, use httppclient. Others’ interfaces cannot provide its configuration. The framework of your own project is spring, and using feign to configure each other is OKHTTpclient. Feign is an interface declarative calling framework that implements an abstract layer of logic, does not really implement the underlying http request, and provides a client interface to implement the underlying http operation. The default implementation is based on http connection, and there is also an implementation based on apachehttpclient, and feign has distributed load balancing function.
2. Use cases
The requirement is to call the functions of the device in another service to be launched in this service, including feign, http, etc., and the http call is selected here.
/** * Super Administrator Authorization * @param userName * @param clientid * @return */ @PostMapping("/mqtt/superuser") @Transactional public Integer loginCheck2(@RequestParam("username") String userName, @RequestParam("clientid") String clientid){ (userName); ("Super Administrator"); userName = "6217XXXXXXXXXXXd85/3XXXX3"; //Split entity class jumps to ibms-iot platform, go online publishConnected(clientid, userName); return 400; }
/** Remotely call the device online function in another service * @param clientid * @param userName */ private void publishConnected(String clientid, String userName) { Connected connected = new Connected(); (ACTION); (clientid); (userName); Date date = new Date(); connected.setConnected_at(()); Map<String, Object> param = (connected, false, true); String url = IotPropertiesConfig.HTTP_PREFIX + IotPropertiesConfig.IP_PORT+ UrlConstant.webHook_path; String result = (url, param, IotPropertiesConfig.HTTP_TIMEOUT); ("equipment:{}Result of online content:{}",(),result); }
httpUtil tool class:
package ; import ; import ; import ; import .*; import ; import .slf4j.Slf4j; import ; import .*; /** * Interface customization tool class */ @Slf4j public class HttpUtils { private static final String CONTENT_TYPE = "Content-Type"; private static final String AUTHORIZATION = "Authorization"; private static final String CONTENT_TYPE_VALUE = "application/x-www-form-urlencoded"; private static final String CONTENT_TYPE_VALUE_JSON = "application/json"; private static ObjectMapper json = new ObjectMapper(); private static ExecutorService cachedThreadPool = (); //Retry interval private static long sleepTime = 1L; //Retry times private static int attemptNumber = 5; //Set the retry mechanism private final static Retryer<String> retryer = RetryerBuilder.<String>newBuilder() .retryIfResult(Predicates.<String>isNull()) // Set custom segment retry source .retryIfExceptionOfType() // Set the exception retry source .retryIfRuntimeException() // Set the exception retry source .withStopStrategy((attemptNumber)) // Set the number of retry times Set the retry timeout time? ? ? ? .withWaitStrategy((sleepTime, )) // Set the interval for each retry .build(); /** * The device is online * @param url * @param paramMap * @param timeout */ public static void deviceOnline(String url, Map<String, Object> paramMap, int timeout) { (new Runnable() { @Override public void run() { postByRetry("",null,1); } }); } /** * * @param url Access path * @param paramMap request body * @param timeout Timeout Unit: seconds * @return * @throws JsonProcessingException */ public static String postByRetry(String url, Map<String, Object> paramMap, int timeout) { Callable<String> task = new Callable<String>() { int i = 0; @Override public String call() throws Exception { i++; if(i > 1){ ("The request failed for the first execution,Beginning{}Time execution!", i); } String result = post(url, paramMap, timeout); return result; } }; String res = ""; try { //Retry the task and get the result returned res = (task); } catch (ExecutionException e) { ("Post ExecutionException", e); } catch (RetryException e) { ("Post RetryException", e); } return res; } /** * * @param url Access path * @param paramMap request body * @param timeout Timeout Unit: seconds * @return * @throws JsonProcessingException */ public static String post(String url, Map<String, Object> paramMap, int timeout) throws JsonProcessingException { String map = (paramMap); String result = HttpRequest .post(url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE).timeout(timeout * 1000) .body(map).execute().body(); return result; } /** * * @param url Access path * @param map request body * @param timeout Timeout Unit: seconds * @return */ public static String post(String url, String map, int timeout) { String result = HttpRequest .post(url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE).timeout(timeout * 1000) .body(map).execute().body(); return result; } /** * * @param url Access path * @param map request body * @param timeout Timeout Unit: seconds * @return */ public static String post(String url, String map, int timeout,String authorization) { String result = HttpRequest .post(url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE).header(AUTHORIZATION,authorization) .timeout(timeout * 1000) .body(map).execute().body(); return result; } /** * * @param url Access path * @param timeout Timeout Unit: seconds * @param authorization authentication token */ public static String get(String url, int timeout,String authorization) { String result = (url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE_JSON).header(AUTHORIZATION,authorization) .timeout(timeout * 1000).execute().body(); return result; } /** * * @param url Access path * @param timeout Timeout Unit: seconds * @param authorization authentication token */ public static String delete(String url, int timeout,String authorization ,String map) { String result = (url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE_JSON).header(AUTHORIZATION,authorization) .timeout(timeout * 1000).body(map).execute().body(); return result; } /** * * @param url Access path * @param timeout Timeout Unit: seconds * @param authorization authentication token */ public static String delete(String url, int timeout,String authorization ) { String result = (url).header(CONTENT_TYPE,CONTENT_TYPE_VALUE_JSON).header(AUTHORIZATION,authorization) .timeout(timeout * 1000).execute().body(); return result; } }
Here publishConnectEd(clientid,userName); uses http to remotely call the interface online on another device in another service.
String url: The interface path that needs to be redirected. (For example: http://localhost:8080/user/login)
param: The required parameters when called remotely.
() Implement http remote call.
Below is the interface that needs to be called remotely
import ; import ; import ; import ; import ; import .*; import ; @RequestMapping("/testDemo") @RestController public class ProductController { @Autowired private ProductService productService; @PostMapping("/save") @Transactional public boolean saveProduct(@RequestBody Product product){ Product result = (product); if (result != null){ return true; }else { return false; } } }
The above is my personal use case. The test was successful and it was used for the first time. If you have any questions, please feel free to correct me.
I hope it will be helpful to everyone's learning and I hope everyone will support me more.