복붙노트

[SPRING] Spring Boot Application에서 Angular 클라이언트가 여러 개인 RequestMapping 문제

SPRING

Spring Boot Application에서 Angular 클라이언트가 여러 개인 RequestMapping 문제

여러 Angular 클라이언트로 전달하려면 일부 GET 요청을 처리해야합니다.

https://example.com/web/index.html    // Client 1
https://example.com/admin/index.html  // Client 2

이후로 / web에 조각난 (#eded) 경로를 사용하고 싶지 않기 때문에 다소 짜증이납니다.

이것은 현재 작동하지 않는 해결책입니다.

@Controller
public class ForwardController {

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public String redirectRoot(HttpServletRequest request) {

        String req = request.getRequestURI();

        if (req.startsWith("/admin/")) {
            return this.redirectAdminTool(request);
        }

        return "forward:/web/index.html";
    }

    @RequestMapping(value = "/web/{path:[^.]*}", method = RequestMethod.GET)
    public String redirectWeb(HttpServletRequest request) {
        return "forward:/web/index.html";
    }

    @RequestMapping(value = "/admin/{path:[^.]*}", method = RequestMethod.GET)
    public String redirectAdminTool(HttpServletRequest request) {
        return "forward:/admin/index.html";
    }
}

이를 통해 어떤 작업이 액세스하고 있습니까?

하지만 작동하지 않는 것은 액세스하는 것입니다.

브라우저를 통해 / web / pricing에 액세스 할 수 있습니다. '새로 고침'을 누르면 모든 것이 작동합니다. 하지만 / web / pricing / vienna는 그렇지 않습니다.

이제 / web / pricing / vienna와 같은 하위 경로를 만들기 위해 요청을 처리하는 방법과 전달 방법을 알아낼 수 없습니다.

이 일을 할 수있는 방법이 있습니까?

@RequestMapping 경로를 / web / **과 같이 변경하면 모든 것이 무한 루프로 끝나고 서버가 중단됩니다.

아마 내가 필요로하는 표현은 다음과 같습니다.

/web(/[^\\.]*)

결과는

MATCH:    /web/pricing
MATCH:    /web/specials/city
MATCH:    /web/specials/city/street
NO MATCH: /web/index.html

그러나 Spring은이 정규식을 좋아하지 않는다 : /web(/[^\\.]*)

결국이 문제는 / web 아래의 정적 리소스를 제외한 모든 것을 일치시킬 수있는 방법을 찾아내는 것으로 귀결됩니다.

해결법

  1. ==============================

    1.내가 끝내었던 것은 다음과 같다 :

    내가 끝내었던 것은 다음과 같다 :

    두 클라이언트를 모두 하위 디렉토리로 이동했습니다. / :

    static/a/web
    static/a/admin
    

    또한 다음과 같이 ForwardController를 구현했습니다.

    @Controller
    public class ForwardController {
    
        @RequestMapping(value = "/*", method = RequestMethod.GET)
        public String redirectRoot(HttpServletRequest request) {
            return "forward:/a/web/index.html";
        }
    
        @RequestMapping(value = "/a/**/{path:[^.]*}", method = RequestMethod.GET)
        public String redirectClients(HttpServletRequest request) {
    
            String requestURI = request.getRequestURI();
    
            if (requestURI.startsWith("/a/admin/")) {
                return "forward:/a/admin/index.html";
            }
    
            return "forward:/a/web/index.html";
        }
    
    }
    
  2. ==============================

    2.모든 경로를 가져 오기 전에 주 색인을로드해야하기 때문에 하위 경로가 작동하지 않습니다. 해결 방법은 서버 측에서 경로를 찾을 수 없을 때마다 기본 색인으로 리디렉션하는 것입니다.

    모든 경로를 가져 오기 전에 주 색인을로드해야하기 때문에 하위 경로가 작동하지 않습니다. 해결 방법은 서버 측에서 경로를 찾을 수 없을 때마다 기본 색인으로 리디렉션하는 것입니다.

    @Controller
    @RequestMapping("/error")
    public class ErrorHandlerController extends AbstractErrorController {
    
    private static final String ROOT_PATH = "/";
    
    public ErrorHandlerController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }
    
    @RequestMapping
    public void errorHtml(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        if(HttpStatus.NOT_FOUND.equals(getStatus(request))) {
            response.sendRedirect(ROOT_PATH);
        }
    }
    
    @Override
    public String getErrorPath() {
        return "error";
    }
    
    }
    
  3. from https://stackoverflow.com/questions/54260546/requestmapping-issue-with-multiple-angular-clients-in-spring-boot-application by cc-by-sa and MIT license