首頁 >Java >java教程 >如何在springboot中使用IDEA遠端連線Debug

如何在springboot中使用IDEA遠端連線Debug

王林
王林轉載
2023-05-10 23:55:041119瀏覽

 1、先建立一個準備遠端偵錯的Demo,注意建置專案的設定

<?xml  version="1.0" encoding="UTF-8"?>
<project 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></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>
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer>
									<resource>META-INF/spring.factories</resource>
								</transformer>
								<transformer>
									<resource>META-INF/spring.schemas</resource>
								</transformer>
								<transformer></transformer>
								<transformer>
									<!--根据项目的全名指定启动类-->
                                    <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> 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> res = new HashMap();
        res.put("code",0);
        res.put("desc","success");
        res.put("data",data);
        return res.toString();
    }
 
    private String fail(){
        Map<string> res = new HashMap();
        res.put("code",1);
        res.put("desc","fail");
        return res.toString();
    }
 
}</string></string></string>

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: Connectionrefused
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]##d

以上是如何在springboot中使用IDEA遠端連線Debug的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除