1、IDEA构建SpringBoot测试Demo
新建SpringBoot测试项目remote-debug,只需要web依赖支持即可;版本选用2.2.6.RELEASE(注意,别用2.5.1版本,有坑,亲测)
1.1、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>2.2.6.RELEASE</version> |
| <relativePath/> |
| </parent> |
| <groupId>com.demo</groupId> |
| <artifactId>remote-debug</artifactId> |
| <version>v1.0</version> |
| <name>remote-debug</name> |
| <description>Demo project for Spring Boot</description> |
| <properties> |
| <java.version>1.8</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-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> |
1.2、application.yml
| server: |
| port: 8087 |
| servlet: |
| context-path: / |
| tomcat: |
| uri-encoding: utf-8 |
1.3、新增Controller,提供接口测试
| package com.java1234.controller; |
| |
| import org.springframework.web.bind.annotation.PostMapping; |
| import org.springframework.web.bind.annotation.RestController; |
| |
| |
| @RestController |
| public class TestController { |
| |
| @PostMapping("/test") |
| public String test(Integer id, String name) { |
| System.out.println("id=" + id); |
| System.out.println("name=" + name); |
| if (id > 0) { |
| return "success " + name; |
| } else { |
| return "fail"; |
| } |
| } |
| } |
1.4、启动项目使用postman测试

2、测试Demo项目配置支持远程调试
2.1、pom.xml里配置jvmArguments参数
| <configuration> |
| <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n</jvmArguments> |
| </configuration> |
整体效果
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-maven-plugin</artifactId> |
| <configuration> |
| <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n</jvmArguments> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
参数描述:
- -Xdebug 通知JVM工作在DEBUG模式下;
- -Xrunjdwp 通知JVM使用(Java debug wire protocol)运行调试环境。该参数同时包含了一系列的调试选项;
- transport 指定了调试数据的传送方式,dt_socket是指用SOCKET模式,另有dt_shmem指用共享内存方式,其中,dt_shmem只适用于Windows平台;
- address 调试服务器的端口号,客户端用来连接服务器的端口号;
- server=y/n VM 是否需要作为调试服务器执行;
- suspend=y/n 是否在调试客户端建立连接之后启动 VM;
3.IDEA打包JAR

4.启动jar并且带启动参数支持远程调试
4.1、把jar存储至随意目录

4.2、启动jar

4.3、启动命令
| java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar remote-debug-v1.0.jar |
| |
5.IDEA远程DEBUG配置


6.IDEA远程DEBUG调试




【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理