Request.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.seec.bok.util;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.Setter;
  7. import org.springframework.boot.web.server.LocalServerPort;
  8. import org.springframework.http.HttpMethod;
  9. import org.springframework.web.client.HttpMessageConverterExtractor;
  10. import org.springframework.web.client.RequestCallback;
  11. import org.springframework.web.client.RestTemplate;
  12. import javax.validation.constraints.NotNull;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. public class Request {
  16. public static int PORT;
  17. private static ObjectMapper mapper = new ObjectMapper();
  18. @LocalServerPort
  19. public void setRandomServerPort(int randomServerPort) {
  20. PORT = randomServerPort;
  21. }
  22. private static RestTemplate RESTTEMPLATE = new RestTemplate();;
  23. @NotNull
  24. HttpMethod requestMethod;
  25. @NotNull
  26. String url;
  27. @NotNull
  28. String path;
  29. @Setter
  30. Object request;
  31. Map<String,Object> urlVariables;
  32. Object resDemo;
  33. public String getRequestJson() throws JsonProcessingException {
  34. return mapper.writeValueAsString(request);
  35. }
  36. public String getResDemoJson() throws JsonProcessingException {
  37. return mapper.writeValueAsString(resDemo);
  38. }
  39. public Request(@NotNull HttpMethod requestMethod, @NotNull String path) {
  40. this.requestMethod = requestMethod;
  41. this.url = "http://localhost:" + PORT +path;
  42. this.path=path;
  43. urlVariables=new HashMap<>();
  44. }
  45. public Object put(String key, Object value) {
  46. return urlVariables.put(key, value);
  47. }
  48. public<T> T execute(Class<T> retType){
  49. RequestCallback requestCallback;
  50. if(request==null){
  51. requestCallback = RESTTEMPLATE.acceptHeaderRequestCallback(retType);
  52. }else {
  53. requestCallback = RESTTEMPLATE.httpEntityCallback(request, retType);
  54. }
  55. HttpMessageConverterExtractor<T> responseExtractor =
  56. new HttpMessageConverterExtractor<T>(retType, RESTTEMPLATE.getMessageConverters());
  57. T ret= RESTTEMPLATE.execute(url,requestMethod,requestCallback,responseExtractor,urlVariables);
  58. resDemo=ret;
  59. return ret;
  60. }
  61. public String OutToString(String methodMessage) throws JsonProcessingException {
  62. return "\n### Request \n" +methodMessage+
  63. "\n#### requestMethod \n" + requestMethod +
  64. "\n#### url \n" + url +
  65. "\n#### path \n" + path +
  66. "\n#### urlVariables \n" + mapper.writeValueAsString(urlVariables) +
  67. "\n#### reqBodyDemo \n``` json\n" + mapper.writeValueAsString(request) +
  68. "\n```" +
  69. "\n#### resBodyDemo \n``` json\n" + mapper.writeValueAsString(resDemo)+
  70. "\n```";
  71. }
  72. }