idea上配置获取
1.配置启动环境参数
配置了两个
from=jar author=test
2.启动类获取参数
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Arrays;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
SpringApplication.run(DemoApplication.class, args);
}
}
启动启动类
3.控制器获取参数
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ParamController {
@Autowired
private ApplicationArguments applicationArguments;
@RequestMapping("/demo/test")
public String test(){
List<String> from = applicationArguments.getNonOptionArgs();
System.out.println(from);
return from.toString();
}
}
启动之后发起请求
jar包运行获取
打包运行
java -jar jar包 from=jar author=test
再发起请求