目录
■例3 ( Spring boot 命令行中,通过jar启动)
■ 坑 !!!结果 to be [.../jre/lib/ext/ ]
■例1 (普通java代码)
・代码1
package com.sxz.test;
public class TestClassLoader {
public static void main(String[] args) {
String classPath = TestClassLoader.class.getClassLoader().getResource("").getPath();
System.out.println(classPath);
}
}
・效果1
/C:/dev/MyJava001/target/classes/
---
・目录1
---
---
■例2 ( Spring boot Eclipse中启动)
・代码2
java处理,调用外系统的 WebAPI(https请求)时,相关知识整理_sun0322的博客-优快云博客
package com.sxz.controler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sxz.entity.TestRequest;
import com.sxz.entity.TestResponse;
@RestController
public class TestController {
@RequestMapping(value = "/createUser", consumes = "application/json;charset=utf-8", produces="application/json;;charset=utf-8")
public TestResponse add(@RequestBody TestRequest test) {
TestResponse response = new TestResponse();
String userName = test.getUserName();
String age = test.getUserAge();
String classPath = TestController.class.getClassLoader().getResource("").getPath();
response.setMessage(userName + "---" + age + "---" + classPath);
response.setStatus("1");
return response ;
}
}
・效果2
・目录2
■例3 ( Spring boot 命令行中,通过jar启动)
启动
===
・效果3
{
"status": "1",
"message": "张三---22---file:/C:/dev/workspace_202106/restFul/target/restFul-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/"
}
===