比来正在做名目的时分,需求java 挪用 python 接口,正在网上找了一些办法,然而总碰着一些成绩,索性将网上的办法以及本人的了解总结一下,心愿对列位有所协助,也请列位年夜神不惜见教。

此办法需求援用 org.python包,需求下载Jpython。正在这里先引见一下Jpython。上面引入百科的诠释:(保举学习:Python视频教程)
Jython是一种完好的言语,而没有是一个Java翻译器或仅仅是一个Python编译器,它是一个Python言语正在Java中的齐全完成。Jython也有不少从CPython中承继的模块库。最风趣的事件是Jython没有像CPython或其余任何初级言语,它提供了对其完成言语的所有存取。以是Jython不只给你提供了Python的库,同时也提供了一切的Java类。这使其有一个微小的资本库。
一:创立环境 Python 环境
import org.python.util.PythonInterpreter;
import java.util.Properties;
/**
* Jython环境,猎取Python的实例
* @author webim
*
*/
public class JythonEnvironment {
//界说一个动态变量
private static JythonEnvironment INSTANCE = new JythonEnvironment();
/**
* 公有结构办法
*/
private JythonEnvironment()
{
}
/**
* 猎取单例
* @return JythonEnvironment
*/
public static JythonEnvironment getInstance()
{
return INSTANCE;
}
/**
* 猎取python诠释器
* @return PythonInterpreter
*/
public PythonInterpreter getPythonInterpreter()
{
Properties props = new Properties();
props.put("python.home","path to the Lib folder");
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false");
Properties preprops = System.getProperties();
//对 python 进行初始化
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter inter = new PythonInterpreter();
return inter;
}
}二:挪用 python 的接口
由于 python 以及 java是两种没有同的言语,因而正在名目的 controller 、service 以及 mapper 中间接呈现 Python 的接口,因而本人封装ExecPython 类,
封装python的接口,目的让 python 接口以及java顺序分离隔。
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Map;
/*enum的这个用法,能够作为变种的平安单例,值患上自创哦 ^_^ */
@Service
@Component
public class ExecPython {
public static final Logger logger = LoggerFactory.getLogger(Exception.class);
//界说 python 诠释器
private static PythonInterpreter inter;
public ExecPython() {
this.inter = JythonEnvironment.getInstance().getPythonInterpreter();
this.inter.execfile("C:\\test.py");
}
//设置 python 剧本的门路
public void setPythonPath (String pythonPath){
this.inter.execfile(pythonPath);
}
public void execute(String scriptFile, Map<String,String> properties)
{
logger.info("猎取诠释器");
try
{
PyFunction getNetInfo = (PyFunction) inter.get("getNetInfo", PyFunction.class);
PyObject netInfo = getNetInfo.__call__();
System.out.println("anwser = " + netInfo.toString());
}
catch (Exception e)
{
e.printStackTrace();
logger.info("Python 剧本文件执行失败");
}
}
//猎取 Python 字符串
public String getString(){
//猎取到python 剧本中的接口
PyFunction func = (PyFunction) inter.get("adder", PyFunction.class);
PyObject pyobj = func.__call__();
System.out.println("anwser = " + pyobj.toString());
return pyobj.toString();
}
// 猎取以后数组
public String getArr() {
PyFunction getArr = (PyFunction) inter.get("getArr", PyFunction.class);
PyObject pyobjTwo = getArr.__call__();
pyobjTwo.__len__();
System.out.println("anwser = " + pyobjTwo.toString()+" len:"+pyobjTwo.__len__());
//将 PyObject 工具转换成 java 工具
//Object object = pyobjTwo.__tojava__(List.class);
//List<String> list = (List<String>) object;
//将查问到数据转换成一个 JSON 字符串
String result = pyobjTwo.toString();
String JsonStr = "{" + result + "}";
logger.info(JsonStr);
logger.info("将查问的后果转换成 JSON 字符串:",JsonStr);
return pyobjTwo.toString();
}
}更多Python相干技巧文章,请拜访Python教程栏目进行学习!
以上就是java后端若何调python接口的具体内容,更多请存眷资源魔其它相干文章!
标签: Python python教程 python编程 python使用问题
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!