[Debug]이미지 DB에서 불러오기[고마운 resourceHandler][MvcConfig]
Programming/Debug

[Debug]이미지 DB에서 불러오기[고마운 resourceHandler][MvcConfig]

 

분명 모델로 잘 담아 왔지만, 사진이 안 불러와 지네요..?

 

 

F12를 켜서 확인해 보니..

 

 

 

역시나..

 

이번엔 400번으로 우리를 혼내줍니다..

 

 

Header를 보니!

 

Request URL이 되게 독특하네요? 이렇게 한적이 없는데..

 

 

알고 보니 제가 데이터를 장바구니 View단에 뿌려줄 때

image만! <img src="{나야, 이미지}">

안에 무작정 모델에서 꺼내서 src에 넣어버렸네요.!

 

이렇게 하면 안 됩니다.!

(단호)

 

(아니! 뭐가 잘못된 걸까?)

 

그것은 바로! 우리가 WebMvcConfig에서

ResourceHandler를 추가를 해줬다는 점!

 

여기서 이미지 자원을 관리하는데, url을 우리가 전에 여기에 지정해 줬다는 겁니다!

 

 

그렇지만.. 제가 srcurl로.. 

 

src 주목!

제가 무자비하게 src에 그냥 넣어버렸습니다!

 

저렇게 하면 이렇게 돼버립니다.

 

 

 

그럼 어떻게 하느냐?

바로 아래처럼! 하면 정상적으로 됩니다!

 

"지정해준 url/데이터" 형태로 src url을 지정해줍니다!

 

 

위 코드를 참고해서 

아래처럼 바꿔주기만 하면!

 

 

정상적을 불러와집니다!!

 

 

Header도 함 볼까요?

 

 

이야.. /upload/ 만 추가해 주었는데 

200번으로 우리를 반겨주네요..!!

 

이쯤 되면 magic 아닙니까? 

resourceHandler가 정말 환상적입니다.

 

ResourceHandler를 공부하고 싶게 하네요!

 

아마 자체적으로 url을 지정을 해두면

 

Handler 친구가 

resourceLocation으로부터 이미지 파일을 찾아내서

요청을 처리해주는 거네요!

 

DB에는 Handler친구가 찾을 이름만 String 값으로 받고

이미지를 보여줄 때는 이 친구가 요청받은 이름만 DB에서

가져와서 file위치에서 찾아주는 거네요!!

 

(정말 고마운 우리 Handler친구)

 

 

Handler친구의 고마움에 보답하기 위해

이 친구에 대해서 배워봅시다!

 

 

ResourceHandlerRegistry의 공식문서를 가져왔습니다.

 

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.html

 

ResourceHandlerRegistry (Spring Framework 5.3.9 API)

Stores registrations of resource handlers for serving static resources such as images, css files and others through Spring MVC including setting cache headers optimized for efficient loading in a web browser. Resources can be served out of locations under

docs.spring.io

 

dam.. 글씨가 너무 작네요.

 

 

직접 복붙 해야겠어요.

 

 

org.springframework.web.servlet.config.annotation

Class ResourceHandlerRegistry

  • java.lang.Object
    • org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry


  • public class ResourceHandlerRegistry extends Object

 

Stores registrations of resource handlers for serving static resources such as images, css files and others through Spring MVC including setting cache headers optimized for efficient loading in a web browser. Resources can be served out of locations under web application root, from the classpath, and others.

To create a resource handler, use addResourceHandler(String...) providing the URL path patterns for which the handler should be invoked to serve static resources (e.g. "/resources/**").

Then use additional methods on the returned ResourceHandlerRegistration to add one or more locations from which to serve static content from (e.g. {"/", "classpath:/META-INF/public-web-resources/"}) or to specify a cache period for served resources.Since:3.1

 

 

와 이 친구.. 대단한 친구네.

 

path만 지정해주면

이미지뿐만 아니라 css도 외부에서 가져올 수 있다네요.

그리고 캐시 헤더 세팅을 통해서 효율적으로 로딩을 한다네요.

amzaing 합니다.

 

 

 

다음에도 file을 가져올 때 잘 쓰이겠네요!

꼭 기억해둡시다!!