一、定义controller
package com. zhk. server. request ;
import lombok. SneakyThrows ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestMethod ;
import org. springframework. web. bind. annotation. RestController ;
import org. springframework. web. method. HandlerMethod ;
import org. springframework. web. servlet. mvc. method. RequestMappingInfo ;
import org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerMapping ;
import java. lang. reflect. Method ;
@RestController
public class RequestController {
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@Autowired
private TestService testService;
@SneakyThrows
@RequestMapping ( "/register" )
public String register ( ) {
RequestMappingInfo mappingInfo = RequestMappingInfo
. paths ( "/web/zhk" )
. methods ( RequestMethod . valueOf ( "GET" ) )
. build ( ) ;
Method test = TestService . class . getDeclaredMethod ( "test" , String . class ) ;
handlerMapping. registerMapping ( mappingInfo, testService, test) ;
return "register::::success" ;
}
@SneakyThrows
@RequestMapping ( "/unregister" )
public String unregister ( ) {
RequestMappingInfo mappingInfo = RequestMappingInfo
. paths ( "/web/zhk" )
. methods ( RequestMethod . valueOf ( "GET" ) )
. build ( ) ;
HandlerMethod handlerMethod = handlerMapping. getHandlerMethods ( ) . get ( mappingInfo) ;
if ( handlerMethod != null ) {
handlerMapping. unregisterMapping ( mappingInfo) ;
}
return "unregisterMapping::::success" ;
}
}
二、添加接口
package com. zhk. server. request ;
import org. springframework. stereotype. Service ;
import org. springframework. web. bind. annotation. ResponseBody ;
@Service
public class TestService {
@ResponseBody
public String test ( String name) {
System . out. println ( "name" + name) ;
return name + "success" ;
}
}