实现Java服务器调用Python服务器进行交互以及数据传输,可采用以下方法,亲测有效:
- 基于Restful风格进行请求调用:
-
框架:
- Java服务器采用Springboot框架进行搭建服务
- python服务器采用FastApi框架进行搭建服务
- 思路:前端–>Java–>python–>Java–>前端
-
代码设计:Axios->@PostMapping->请求Python(下文描述)–>@app.get/@app.Post
- 请求Python方式:Java可以采用Spring框架的RestTemplate进行请求Python服务器,或者使用原生JavaEE进行HttpRequest请求访问Python服务器。
- 以下将描述两种调用的方法:
1.基于RestTemplate进行请求Python服务器
Java:
@GetMapping("/api/length")
public String getAiAssistant(String text) {
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
text="我要喝咖啡!!";
Map<String,String> requestBody =new HashMap<>();
requestBody.put("text",text);
requestBody.put("text2","再来一个~~~");
// 设置请求体数据
// requestBody = "{\"text\": \"" + text + "\"}";
// String requestBody2 = "{\"text2\": \"" + text + "\"}";
String url = "http://192.168.1.11:8000/api/length";
String pythonUrl = jackieConfig.getPythonUrl();
// 创建HttpEntity对象,将请求头和请求体封装起来
//可以基于这个进行修改
// HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
HttpEntity<Map> requestEntity = new HttpEntity<>(requestBody, headers);
System.out.println("requestEntity = " + requestEntity +"+类型:"+requestEntity.getClass());
// 发送POST请求,并获取响应结果
RestTemplate restTemplate = new RestTemplate();
System.out.println("restTemplate = " + restTemplate+"+类型:"+restTemplate.getClass());
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class);
System.out.println("responseEntity = " + responseEntity +"+类型:"+responseEntity.getClass());
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String response = responseEntity.getBody();
System.out.println("response = " + response+"+类型:"+response.getClass());
return response;
} else {
// 处理请求失败的情况
return null;
}
}
python:
# 通过响应体传递参数
@app.post("/api/length")
def get_length(data: dict):
print(data)
text = data["text"]
length = len(text)
return {"data": data}
2.基于HttpRequest进行请求Python服务器
Java:
@PostMapping("/page")
public String DocGeneration( String data) {
data="我要喝咖啡!!";
SysUser TenantUser = SecurityUtils.getLoginUser().getUser();
SysTenant sysTenant = sysTenantService.selectSysTenantById(TenantUser.getTenantId());
String apiKey = sysTenant.getApiKey();
String apiStatus = String.valueOf(sysTenant.getApiStatus());
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("apiKey",apiKey);
paramMap.put("model", apiStatus);
paramMap.put("context", data);
String stringbody = String.valueOf(new JSONObject(paramMap));
String responseBody =null;
try
{
responseBody = HttpRequest.post(pythonUrl + "/docGeneration/page")
.header("Authorization", apiKey)
.header("Content-Type", "application/json")
.body(stringbody)
.execute()
.body();
System.out.println("responseBody = " + responseBody);
} catch (HttpException e) {
return "出现了异常";
} catch (ConvertException e) {
return "出现了异常";
}
System.out.println("数据对接");
return responseBody;
}
java返回数据:
python返回数据:
文章来源:https://www.toymoban.com/news/detail-858565.html
Python:文章来源地址https://www.toymoban.com/news/detail-858565.html
class Data(BaseModel):
apiKey: str
context: str
model: str
@app.post("/docGeneration/page")
async def docGeneration(data: Data):
print(data)
return {"data": data}
到了这里,关于Java服务器调用Python服务器进行交互:基于Http协议的Restful风格调用(Springboot/FastApi)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!