首页 > Java > java教程 > 正文

如何在springboot中使用IDEA远程连接Debug

王林
发布: 2023-05-10 23:55:04
转载
1537人浏览过

 1、先创建一个准备远程调试的Demo,注意构建项目的配置

<?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.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.remote.test</groupId>
	<artifactId>remote_test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>remote_test</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</artifactId>
		</dependency>
 
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>RELEASE</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.2</version>
				<dependencies>
					<dependency>
						<groupId>org.springframework.boot</groupId>
						<artifactId>spring-boot-maven-plugin</artifactId>
						<version>2.1.4.RELEASE</version>
					</dependency>
				</dependencies>
				<configuration>
					<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
					<createDependencyReducedPom>false</createDependencyReducedPom>
					<filters>
						<filter>
							<artifact>*:*</artifact>
							<excludes>
								<exclude>META-INF/*.SF</exclude>
								<exclude>META-INF/*.DSA</exclude>
								<exclude>META-INF/*.RSA</exclude>
							</excludes>
						</filter>
					</filters>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<finalName>${project.artifactId}-${project.version}-all</finalName>
							<transformers>
								<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
									<resource>META-INF/spring.factories</resource>
								</transformer>
								<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<!--根据项目的全名指定启动类-->
                                    <mainClass>com.remote.test.remote_test.RemoteTestApplication</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
登录后复制
package com.remote.test.remote_test;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.HashMap;
import java.util.Map;
 
 
@RestController
@RequestMapping("remote/test")
public class UserController {
 
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
 
 
    @PostMapping("selectByUserId")
    public String selectUserInfo(@RequestParam("userId") String userId) {
        try {
            Map<String,Object> userInfo = new HashMap<>();
            userInfo.put("userId",userId);
            userInfo.put("age",23);
            userInfo.put("name","yanshao");
            userInfo.put("address","shanghai");
            logger.info("Query user information by user ID. userInfo: {}",userInfo.toString());
            return this.success(userInfo);
        } catch (Exception e) {
            logger.error("Query user information by user ID. userId:{} ", userId, e);
            return this.fail();
        }
    }
 
    private String success(Object data){
        Map<String,Object> res = new HashMap<>();
        res.put("code",0);
        res.put("desc","success");
        res.put("data",data);
        return res.toString();
    }
 
    private String fail(){
        Map<String,Object> res = new HashMap<>();
        res.put("code",1);
        res.put("desc","fail");
        return res.toString();
    }
 
}
登录后复制

2、打包

输入:mvn clean package,(大概需要等几分钟),最好在构建之前指定本地repository,就不需要重新下载jar包了。

如何在springboot中使用IDEA远程连接Debug

如何在springboot中使用IDEA远程连接Debug

3、在IDEA配置远程Debug

指定socket port = 8081,指定准备debug的模块

如何在springboot中使用IDEA远程连接Debug

4、在终端启动刚才打好的jar包

a. 先在IDEA启动debug

如何在springboot中使用IDEA远程连接Debug

b. 然后在终端输入命令:java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0.1-SNAPSHOT-all.jar

如何在springboot中使用IDEA远程连接Debug

5、测试

在准备请求的接口上标记断点

如何在springboot中使用IDEA远程连接Debug

如何在springboot中使用IDEA远程连接Debug

如何在springboot中使用IDEA远程连接Debug

注意:必须先在IDEA启动Debug,然后再启动项目

➜ desktop java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0.1-snapshot-all.jar

error: transport error 202: connect failed: connection refused
error: jdwp transport dt_socket failed to initialize, transport_init(510)
jdwp exit error agent_error_transport_init(197): no transports initialized [debuginit.c:750]

以上就是如何在springboot中使用IDEA远程连接Debug的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:亿速云网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号