在实际工作中,特别是接手别人的项目的时候,我们经常会涉及到首先筛选当前项目有哪些接口,然后按照业务进行筛选规划后期的工作安排。因此那么如何快速的查看当前springboot项目有哪些接口呢?其实很简单,把下面的代码粘贴到项目里面去:
package org.shop.oauth.server.controller; import java.util.List; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @RestController @RequestMapping("/monitoring/") public class MonitoringController { @Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping; @GetMapping("/endpoints") public ResponseEntity<List<String>> getEndpoints() { return new ResponseEntity<>( requestMappingHandlerMapping .getHandlerMethods() .keySet() .stream() .map(RequestMappingInfo::toString) .collect(Collectors.toList()), HttpStatus.OK ); } }
然后把项目运行起来,访问这个接口地址,就可以看到整个springboot项目暴露了哪些接口了。
还没有评论,来说两句吧...