创建接口
创建com.wz.nls.business.controller
在controller包下创建TestController.java
java
package com.wz.nls.business.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
private String hello() {
return "hello world";
}
}
创建http测试
在根目录下创建http文件夹,创建test.http文件
键入gtr,自动补全
java
GET http://localhost:8080/hello
Accept: application/json
###
启动主程序,测试
GET http://localhost:8080/hello
HTTP/1.1 200
Content-Type: application/json
Content-Length: 11
Date: Mon, 29 Dec 2025 12:22:13 GMT
hello world
响应文件已保存。
> 2025-12-29T202213.200.json
Response code: 200; Time: 72ms (72 ms); Content length: 11 bytes (11 B)
修改运行参数
运行参数在/data/wz/JavaProject/im-nls/business/src/main/resources/application.properties中
java
spring.application.name=business
server.servlet.context-path=/nls
server.port=18000
修改/data/wz/JavaProject/im-nls/business/src/main/java/com/wz/nls/business/BusinessApplication.java
增加getEnvironment,能够environment.getProperty获取运行参数
ctrl+alt+v自动生成变量,并引入log,打印log到控制台
java
package com.wz.nls.business;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
@Slf4j
@SpringBootApplication
public class BusinessApplication {
public static void main(String[] args) {
ConfigurableEnvironment environment = SpringApplication.run(BusinessApplication.class, args).getEnvironment();
log.info("启动成功!");
log.info("测试地址:http://localhost:{}{}/hello",
environment.getProperty("server.port"),
environment.getProperty("server.servlet.context-path"));
}
}
同时修改http测试
java
GET http://localhost:18000/nls/hello
Accept: application/json