- 在Eclipse中创建springboot工程(maven)
- 配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> </dependency> <dependency> <groupId>org.python</groupId> <artifactId>jython-slim</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- 编写python代码 /src/main/resources/case.py
class RequestObj: def __init__(self,requestId): self.requestid=requestId class ResponseObj: def __init__(self,apiId): self.apiId=apiId def testAPI(requestObj): if requestObj.requestId == "1" : responseObj=ResponseObj("/test/1") elif requestObj.requestId == "2" : responseObj=ResponseObj("/test/2") else: responseObj=ResponseObj("/test") return responseObj
- 编写java代码 /src/main/java/com/example/demo/json/Request.java
package com.example.demo.json; public class Request { private String requsetId; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRequsetId() { return requsetId; } public void setRequsetId(String requsetId) { this.requsetId = requsetId; } }
/src/main/java/com/example/demo/json/Response.java
package com.example.demo.json; public class Response { private String apiId; public String getApiId() { return apiId; } public void setApiId(String apiId) { this.apiId = apiId; } }
/src/main/java/com/example/demo/RestAPIController.java
package com.example.demo; import java.io.IOException; import org.python.core.PyFunction; import org.python.core.PyObject; import org.python.core.PyString; import org.python.util.PythonInterpreter; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.example.demo.json.Request; import com.example.demo.json.Response; @RestController public class RestAPIController { private static PythonInterpreter interpreter; private static Resource resource = new ClassPathResource("case.py"); private static String path; static { interpreter = new PythonInterpreter(); try { path = resource.getFile().getPath(); } catch (IOException e) { e.printStackTrace(); } } private Response callPythonFunc(String funcName, Request request) { interpreter.execfile(path); PyObject requestObj = interpreter.get("RequestObj", PyObject.class); requestObj.__setattr__("requestId", new PyString(request.getRequsetId())); PyFunction function = interpreter.get(funcName, PyFunction.class); PyObject responseObj = function.__call__(requestObj); String apiId = responseObj.__getattr__("apiId").toString(); Response response = new Response(); response.setApiId(apiId); return response; } @GetMapping("/test/get") public String test1() { Request request = new Request(); request.setRequsetId("1"); Response response = callPythonFunc("testAPI", request); return response.getApiId(); } @PostMapping("/test/post") public Response test3(@RequestBody Request request) { Response response = callPythonFunc("testAPI", request); return response; } }
- 编写json
{ "requsetId":1, "name":"myname" }
- 启动postman 测试RESTAPI ,URL⇒http://localhost:8080/test/post BODY⇒上面的json 点击”send”
- response如下
{ "apiId": "/test/1" }