开发日记2-创建http文件测试http接口

创建接口

创建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
相关推荐
handsomestWei1 天前
Docker引擎API接入配置
运维·http·docker·容器·api
广州灵眸科技有限公司1 天前
为RK3588注入澎湃算力:RK1820 AI加速卡完整适配与评测指南
linux·网络·人工智能·物联网·算法
byoass1 天前
csdn_upload_005
网络·安全·云计算
IT WorryFree1 天前
飞塔防火墙与第三方设备进行IPSEC故障诊断期间,用户可能会观察到以下错误:
linux·服务器·网络
不是书本的小明1 天前
K8S应用优化方向
网络·容器·kubernetes
~plus~1 天前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
w6100104661 天前
CKA-2026-Service
linux·服务器·网络·service·cka
GTgiantech1 天前
灵活拓展网络边界:电口光模块的智慧选型与部署指南
网络
汤愈韬1 天前
网络安全之网络基础知识_2
网络协议·安全·web安全
测试专家1 天前
天脉3操作系统
网络