文章目录
- application.yml配置
- 封装Result返回类
- 调用Python
application.yml配置
server:
port: 9000
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
cache: false # 模板热部署、禁用 thymeleaf 缓存
mode: HTML
suffix: .html
prefix: classpath:/templates/
encoding: UTF-8
servlet:
content-type: text/html
mvc:
static-path-pattern: /static/** # 添加static文件夹下其他文件夹可访问
mybatis:
type-aliases-package: com.sxf.web.mapper
mapper-locations: classpath:mapper/*.xml
封装Result返回类
package com.scau.demo.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Result extends HashMap<String, Object> {
@Override
public Result put(String key, Object value){
super.put(key, value);
return this;
}
public Result(){
}
public static Result error(int code, String msg){
Result r = new Result();
r.put("code", code);
r.put("msg", msg);
return r;
}
public static Result error(){
return error(500, "未知异常,请联系管理员");
}
public static Result error(String msg){
return error(500, msg);
}
public static Result ok(Map<String, Object> map){
Result r = new Result();
r.put("code", 0);
r.putAll(map);
return r;
}
public static Result ok(List<Map<String,Object>> list){
Result r = new Result();
r.put("code", 0);
r.put("msg", list);
return r;
}
public static Result ok(){
Result r = new Result();
r.put("code", 0);
r.put("msg", "操作成功");
return r;
}
public static Result ok(Object msg) {
Result r = new Result();
r.put("code", 0);
r.put("msg", msg);
return r;
}
}
使用时:
public Result get(String openid) {
Result result = userMapper.get(openid);
if (result != null)
return Result.ok(result);
else
return Result.error("异常");
}
@RequestMapping("/test")
public Result test() {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>(2);
map.put("itemId", 1);
list.add(map);
map = new HashMap<>(2);
map.put("itemId", 2);
list.add(map);
return new Result().ok(list);
}
调用Python
public String python() {
try {
String[] args = new String[] {"python", "tool.py"};
Process process = Runtime.getRuntime().exec(args);
BufferedReader successMsg = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader errorMsg = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line=successMsg.readLine())!=null) {
System.out.println(line);
}
while ((line=errorMsg.readLine())!=null) {
System.out.println(line);
}
successMsg.close();
errorMsg.close();
process.waitFor();
}catch (Exception e){
e.printStackTrace();
}
return "ok";
}
中文乱码解决方法:中文乱码
官网:https://www.jython.org/download 精简版jar:jython-compile-maven-plugin-test-1.2-jar-with-dependencies.jar
// Python面向函数式编程: 在Java中调用Python函数
PythonInterpreter interpreter = new PythonInterpreter();
// 加载Python程序
interpreter.execfile("D:\\add.py");
// 调用Python程序中的函数
// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
int a = 5, b = 10;
//调用函数,如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”
PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("the anwser is: " + pyobj);