Home  >  Article  >  Java  >  How to use IDEA remote connection Debug in springboot

How to use IDEA remote connection Debug in springboot

王林
王林forward
2023-05-10 23:55:04995browse

1. First create a Demo ready for remote debugging, pay attention to the configuration of the build project



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.remote.test
	remote_test
	0.0.1-SNAPSHOT
	remote_test
	Demo project for Spring Boot
 
	
		1.8
	
 
	
		
			org.springframework.boot
			spring-boot-starter
		
 
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.junit.jupiter
			junit-jupiter-api
			RELEASE
			test
		
	
 
	
		
			
				org.apache.maven.plugins
				maven-shade-plugin
				2.2
				
					
						org.springframework.boot
						spring-boot-maven-plugin
						2.1.4.RELEASE
					
				
				
					true
					false
					
						
							*:*
							
								META-INF/*.SF
								META-INF/*.DSA
								META-INF/*.RSA
							
						
					
				
				
					
						package
						
							shade
						
						
							${project.artifactId}-${project.version}-all
							
								
									META-INF/spring.handlers
								
								
									META-INF/spring.factories
								
								
									META-INF/spring.schemas
								
								
								
									
                                    com.remote.test.remote_test.RemoteTestApplication
								
							
						
					
				
			
		
	
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 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 res = new HashMap();
        res.put("code",0);
        res.put("desc","success");
        res.put("data",data);
        return res.toString();
    }
 
    private String fail(){
        Map res = new HashMap();
        res.put("code",1);
        res.put("desc","fail");
        return res.toString();
    }
 
}

2. Packaging

Input: mvn clean package, (probably required Wait a few minutes), it is best to specify the local repository before building, so there is no need to re-download the jar package.

How to use IDEA remote connection Debug in springboot

How to use IDEA remote connection Debug in springboot

3. Configure remote debug in IDEA

Specify socket port = 8081 and specify the port to be debugged Module

How to use IDEA remote connection Debug in springboot

4. Start the jar package you just created in the terminal

a. First start debug in IDEA

How to use IDEA remote connection Debug in springboot

#b. Then enter the command in the terminal: java -agentlib:jdwp=transport=dt_socket,server=n,address=localhost:8081 -jar remote_test-0.0. 1-SNAPSHOT-all.jar

How to use IDEA remote connection Debug in springboot

5. Test

Mark breakpoint on the interface preparing the request

How to use IDEA remote connection Debug in springboot

How to use IDEA remote connection Debug in springboot

How to use IDEA remote connection Debug in springboot

Note: You must start Debug in IDEA first, and then start the project

➜ 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]

The above is the detailed content of How to use IDEA remote connection Debug in springboot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete