文心一言接入流程
一、定义文心接口相关的入参、出参类
public class WenXinMsg { private String role = "user"; private String content; public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
import java.util.List; public class WenXinMsgReq { private List<WenXinMsg> messages; public List<WenXinMsg> getMessages() { return messages; } public void setMessages(List<WenXinMsg> messages) { this.messages = messages; } }
public class WenXinMsgRes { private String result; private Integer error_code; private String error_msg; public Integer getError_code() { return error_code; } public void setError_code(Integer error_code) { this.error_code = error_code; } public String getError_msg() { return error_msg; } public void setError_msg(String error_msg) { this.error_msg = error_msg; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } }
public class WenXinToken { private String access_token; public String getAccess_token() { return access_token; } public void setAccess_token(String access_token) { this.access_token = access_token; } }
二、编写工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import java.util.Map; /** * RestTemplateUtil工具类 */ @Component public class RestTemplateUtil { @Autowired private RestTemplate restTemplate; /** * post 请求 * * @param url 请求路径 * @param data body数据 * @return */ public String post(String url, String data) { HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.add("Content-Encoding", "UTF-8"); headers.add("Content-Type", "application/json; charset=UTF-8"); HttpEntity<String> requestEntity = new HttpEntity<>(data, headers); return restTemplate.postForObject(url, requestEntity, String.class); } /** * get 请求 * * @param url 请求路径 * @param token JWT所需的Token,不需要的可去掉 * @return */ public String get(String url, String token) { HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "application/json"); headers.add("Content-Encoding", "UTF-8"); headers.add("Content-Type", "application/json; charset=UTF-8"); if (token != null) { headers.add("Authorization", token); } HttpEntity<String> requestEntity = new HttpEntity<>(null, headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); String responseBody = response.getBody(); return responseBody; } /** * get 请求 * * @param url 请求路径 * @return */ public <T> T getWithSession(String url, String sessionId, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("session_id", sessionId); return get(url, httpHeaders, responseType, uriVariables).getBody(); } // ----------------------------------GET------------------------------------------------------- /** * GET请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, Class<T> responseType) { return restTemplate.getForEntity(url, responseType); } /** * GET请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, Class<T> responseType, Object... uriVariables) { return restTemplate.getForEntity(url, responseType, uriVariables); } /** * GET请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.getForEntity(url, responseType, uriVariables); } /** * 带请求头的GET请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return get(url, httpHeaders, responseType, uriVariables); } /** * 带请求头的GET请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) { HttpEntity<?> requestEntity = new HttpEntity<>(headers); return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables); } /** * 带请求头的GET请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return get(url, httpHeaders, responseType, uriVariables); } /** * 带请求头的GET请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> get(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<?> requestEntity = new HttpEntity<>(headers); return exchange(url, HttpMethod.GET, requestEntity, responseType, uriVariables); } // ----------------------------------POST------------------------------------------------------- /** * POST请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @return */ public <T> ResponseEntity<T> post(String url, Class<T> responseType) { return restTemplate.postForEntity(url, HttpEntity.EMPTY, responseType); } /** * POST请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType) { return restTemplate.postForEntity(url, requestBody, responseType); } /** * POST请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Object... uriVariables) { return restTemplate.postForEntity(url, requestBody, responseType, uriVariables); } /** * POST请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.postForEntity(url, requestBody, responseType, uriVariables); } /** * 带请求头的POST请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return post(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的POST请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return post(url, requestEntity, responseType, uriVariables); } /** * 带请求头的POST请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return post(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的POST请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return post(url, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的POST请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) { return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的POST请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> post(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, uriVariables); } // ----------------------------------PUT------------------------------------------------------- /** * PUT请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, Class<T> responseType, Object... uriVariables) { return put(url, HttpEntity.EMPTY, responseType, uriVariables); } /** * PUT请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody); return put(url, requestEntity, responseType, uriVariables); } /** * PUT请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody); return put(url, requestEntity, responseType, uriVariables); } /** * 带请求头的PUT请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return put(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的PUT请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return put(url, requestEntity, responseType, uriVariables); } /** * 带请求头的PUT请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return put(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的PUT请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return put(url, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的PUT请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) { return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的PUT请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> put(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables); } // ----------------------------------DELETE------------------------------------------------------- /** * DELETE请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Class<T> responseType, Object... uriVariables) { return delete(url, HttpEntity.EMPTY, responseType, uriVariables); } /** * DELETE请求调用方式 * * @param url 请求URL * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Class<T> responseType, Map<String, ?> uriVariables) { return delete(url, HttpEntity.EMPTY, responseType, uriVariables); } /** * DELETE请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody); return delete(url, requestEntity, responseType, uriVariables); } /** * DELETE请求调用方式 * * @param url 请求URL * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody); return delete(url, requestEntity, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers); return delete(url, requestEntity, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers); return delete(url, requestEntity, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return delete(url, requestEntity, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, Map<String, String> headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAll(headers); return delete(url, httpHeaders, requestBody, responseType, uriVariables); } /** * 带请求头的DELETE请求调用方式 * * @param url 请求URL * @param headers 请求头参数 * @param requestBody 请求参数体 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) { HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers); return delete(url, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的DELETE请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) { return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables); } /** * 自定义请求头和请求体的DELETE请求调用方式 * * @param url 请求URL * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> delete(String url, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables); } // ----------------------------------通用方法------------------------------------------------------- /** * 通用调用方式 * * @param url 请求URL * @param method 请求方法类型 * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,按顺序依次对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) { return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables); } /** * 通用调用方式 * * @param url 请求URL * @param method 请求方法类型 * @param requestEntity 请求头和请求体封装对象 * @param responseType 返回对象类型 * @param uriVariables URL中的变量,与Map中的key对应 * @return ResponseEntity 响应对象封装类 */ public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) { return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables); } }
import java.util.*; @Component public class WenXinUtil { private String appID = ""; private String apiKey = ""; private String secretKey = ""; @Autowired RestTemplateUtil restTemplateUtil; public String aiChat(String input) { System.out.println(input); String tokenUrl = String.format("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s&",apiKey,secretKey); Map<String,String> mapHeader = new HashMap<>(); mapHeader.put("Content-Type","application/json"); mapHeader.put("Accept","application/json"); ResponseEntity<WenXinToken> tokenEntity = restTemplateUtil.post(tokenUrl,mapHeader, HttpEntity.EMPTY,WenXinToken.class); if (tokenEntity != null) { String accessToken = tokenEntity.getBody().getAccess_token(); String getChatUrl = String.format("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=%s",accessToken); WenXinMsg wenXinMsg = new WenXinMsg(); wenXinMsg.setContent(input); WenXinMsgReq wenXinMsgReq = new WenXinMsgReq(); List<WenXinMsg> messages = new ArrayList<>(); messages.add(wenXinMsg); wenXinMsgReq.setMessages(messages); ResponseEntity<LinkedHashMap> wenXinMsgResResponse = restTemplateUtil.post(getChatUrl,mapHeader,wenXinMsgReq, LinkedHashMap.class); if (wenXinMsgResResponse != null && wenXinMsgResResponse.getBody() != null) { if (wenXinMsgResResponse.getBody() != null) { System.out.println(wenXinMsgResResponse.getBody()); if (wenXinMsgResResponse.getBody().containsKey("error_code")) { return wenXinMsgResResponse.getBody().get("error_msg").toString(); } return wenXinMsgResResponse.getBody().get("result").toString(); } else { return "error"; } //return wenXinMsgResResponse.getBody().getResult(); } } return ""; } }
三、编写对外接口
@RequestMapping("/wenxin")
@RestController
public class WenXinController { @Autowired WenXinUtil wenXinUtil; @PostMapping("/chat")
@ResponseBody public String introduce(@RequestBody String input) { String aiMsg = wenXinUtil.aiChat(input); return aiMsg; } }