1
0

Spring Boot でマッピングされているURLの一覧を確認したい

Posted at

対象環境:

  • JAVA 11.0.24 (corretto-11)
  • Spring Boot v2.3.12.RELEASE

https://qiita.com/otsu-Miya/items/eac37b2720b67a58690b
を参考に、対象環境にてマッピングをlogに表示してみた。

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
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.util.Map;

@Controller
@RequestMapping("/")
@Slf4j
public class HogeController {

    private final RequestMappingHandlerMapping handlerMapping;

    public HogeController(RequestMappingHandlerMapping handlerMapping) {
        this.handlerMapping = handlerMapping;
    }

    @GetMapping("test")
    public ResponseEntity<String> test() throws ClassNotFoundException {
        log.info("======= url mapping");
        Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = this.handlerMapping.getHandlerMethods();
        for( Map.Entry<RequestMappingInfo, HandlerMethod> mapItem : handlerMethodMap.entrySet() ) {
            RequestMappingInfo key = mapItem.getKey();
            HandlerMethod value = mapItem.getValue();
            log.info("======= " + key.getMethodsCondition() + " " + key.getPatternsCondition() + " :: " + value);
        }

        return ResponseEntity.ok().body("OK");
    }

}

実行して /test にリクエストすれば log の設定場所に表示されるはず。json とかで返しても良かったかなと思ったけどjavaでjson返すの面倒だったことを思い出した。

面倒だな、java/spring...

spring framework の標準機能で route list 出させろよってなってる

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0