성윤님이 코드로 등록해 로그를 찍어 공개하는 바람에 김이 빠져버린 퀴즈. 원래 문제만 보고 푸는 퀴즈였는데…

어쨌든 답을 정리하고 설명해보자. 매번 로그 찍어가며 원하는 요청 조건을 찾아낼 수는 없으니까 각 요청조건의 적용 규칙을 알아야지.

@RequestMapping의 각 항목은 RequestCondition 타입으로 만들어진 요청조건 클래스로 정의된다. @RequestMapping의 엘리먼트 타입은 모두 배열이니 {}를 이용해 복수개 설정이 가능하다. 또, 타입레벨과 메소드 레벨 양쪽에 정의가 가능하다. 최종 적용은 메소드 단위이니 타입 레벨의 조건과 어떻게 결합되는지 알아야 한다. 여기에 수퍼 클래스나 인터페이스의 타입&메소드 레벨 조건도 결합할 수 있으니 단순하게 봐도 조합 방법이 엘리먼트 값 여러 개 * 메소드&타입 * 수퍼 타입&메소드 해서 최소 8가지. 문제는 요청조건마다 엘리먼트 값의 조합이나 메소드&타입 조합방법이 다르다는 것. 

문제를 살펴보자.

1.
@Controller
@RequestMapping({“/a”, “/b”})
public class MyController {
  @RequestMapping({“/c”, “/d”})
  public void hello() {}

이건 URL경로 조건. 디폴트 엘리먼트라 value로 해도 되고, 단독으로 사용되면 엘리먼트 이름은 생략 가능.

각 엘리먼트 값의 조합은 OR. 메소드만 놓고 보면 /c 또는 /d.

타입과 메소드의 조합도 OR이지만 단순히 a,b,c,d로 조합하는게 아니라 타입 경로+메소드 경로로 결합 가능한 모든 경우로 조합한다는 것이 특징이다. 결국 /a/c, /a/d, /b/c, /b/d의 네 가지 조합이 만들어지고 OR로 결합되니 이 중의 하나면 된다. AND라면 말이 안되니까. 한가지 주의할 점은 다른 조건과 다르게 URL을 조건을 지정안 한다고 조건을 다 만족하는게 아니다. URL조건이 없으면 ""인데 suffix / 규칙 때문에 "/”나 마찬가지다. 결국 서블릿 매핑의 루트 URL에 연결된다.

최종 요청조건은 {[/a/c || /a/d || /b/c || /b/d], methods=[], params=[], headers=[], consumes=[], produces=[], custom=[]} 으로 표현된다. 스프링 로그를 찍어보면 조합된 조건이 이렇게 간결한 식으로 나온다.

2.
@Controller
@RequestMapping(params={“a”, “b”})
public class MyController {
  @RequestMapping(params={“c”, “d”})
  public void hello() {}

파라미터 조합은 AND다. 엘리먼트 값들도 AND로. 타입, 메소드도 AND로 따라서 최종 조건은 한 가지다. 위의 매핑이라면 파라미터에 a,b,c,d 네 가지가 다 존재해야지만 조건 만족.

조건식은 {[], methods=[], params=[a && b && c && d], headers=[], consumes=[], produces=[], custom=[]}

3.
@Controller
@RequestMapping(headers={“a”, “b”})
public class MyController {
  @RequestMapping(headers={“c”, “d”})
  public void hello() {}

헤더 조건도 파라미터와 동일하게 타입, 메소드, 엘리먼트 값이 모두 AND로 조합된다. 매핑 가능한 요청조건은 한 가지.

조건식은 {[], methods=[], params=[], headers=[a && b && c && d], consumes=[], produces=[], custom=[]}

여기까지는 쉬운데 다음부터는 좀 정확한 이해가 필요하다.

4.
@Controller
@RequestMapping(headers={“a”, “Content-Type=application/json”, “Content-Type=multipart/form-data”})
public class MyController {
  @RequestMapping(headers={“c”, “d”})
  public void hello() {}

타입에 헤더 조건이 3개, 메소드에 2개다. 헤더 조건은 AND 조합이라고 했으니 결국 조건은 한 가지?

아니다. 두 가지다. header로 정의했더라도 Context-Type은 무시된다. 헤더 조건은 a,c,d 세 가지만 적용된다. Content-Type은 대신 3.1에 새로 등장한 consumes 조건으로 바뀐다. 따라서 타입에 붙은 두 개의 Content-Type 헤더 조건 두 가지는 consumes에 정의된 것으로 취급되는데, consumes는 헤더 값을 이용하지만 다른 헤더와 달리 AND가 아니라 OR조건이다. 따라서 헤더 조건 3가지가 AND로 묶이고, Content-Type 두 가지는 OR로 묶여서 최종적으로 2가지 요청조건이 만들어진다.

Content-Type은 headers에서 무시하고 몰래 consumes로 처리하는 이유는 뭔가? headers에서 안 쓸 거면 에러를 내든가하지. 왜냐하면 3.0에서 작성된 코드를 3.1에서 사용될 수 있도록 하기 위해서다.  구버전 호환성을 목숨처럼 지키는 스프링 다운 해결책이다.

조건식은 {[], methods=[], params=[], headers=[a && c && d], consumes=[application/json || multipart/form-data], produces=[], custom=[]}

5.
@Controller
@RequestMapping(headers={“a”, “b”})
public class MyController {
  @RequestMapping(headers={“c”, “d”, “Content-Type=application/json”}, consumes=”multipart/form-data”)
  public void hello() {}

4번 문제의 응용이다. 일반 헤더 조건은 AND로 결합해서 한 가지. Content-Type헤더는 consumer로 넘어가므로 consumes가 결국 두 가지가 된다. consumes는 OR이니까 최종 요청조건은 두 가지.

조건식은 {[], methods=[], params=[], headers=[a && b && c && d], consumes=[application/json || multipart/form-data], produces=[], custom=[]}

6.
@Controller
@RequestMapping(consumes={“application/xml”, “application/x-www-form-urlencoded”})
public class MyController {
  @RequestMapping(consumes={”multipart/form-data”, “application/json”})
  public void hello() {}

consumes는 다른 조건과 다르게 메소드 조건이 타입 조건을 오버라이딩하는 식이다. 타입만 있으면 타입 조건을 쓰지만 메소드 조건이 있으면 타입 조건을 무시한다. 그래서 이건 메소드 조건 두 가지만 OR로 결합되서 두 가지.

조건식은 {[], methods=[], params=[], headers=[], consumes=[multipart/form-data || application/json], produces=[], custom=[]}

 

정답은 4,1,1,2,2,2

 

그럼 수퍼 클래스나 인터페이스의 타입&메소드 레벨에 붙은 @RequestMapping과의 조합은? 그건 http://toby.epril.com/?p=1207 에서 설명한 스프링의 일반적인 오버라이딩스러운 규칙을 적용하면 된다.

Related posts:

  1. 스프링 3.1 (10) 심심풀이 @RequestMapping 요청 조건 퀴즈
  2. Inside Spring (6) 애노테이션 설정 지원 스프링 웹 테스트용 DispatcherServlet 만들기
  3. Spring 상식퀴즈 (1) – DI 태클하기
  4. Spring 3.0 EL (Spel)을 이용한 AssertThrows 확장 (2)
  5. S1A 2008 둘째날 – Grails
  6. 토비의 스프링 3 간략 목차
  7. 스프링 빈의 이름은 한글로 지어도 된다
  8. Spring 3.0 (58) Static Class를 XML없이 빈으로 등록하기
  9. Spring 3.0 EL (Spel)을 이용한 AssertThrows 확장 (1)
  10. 스프링 3.1 (8) web.xml 없는 스프링 개발
  11. TheSpringExperience 2007 둘째날
  12. Spring 2.0 XML확장기능 (3)
  13. Spring 3.0 @MVC 메소드에서 자동으로 리턴 모델에 추가되는 것들
  14. Spring ROO 대충대충 분석 (2) ROO란 무엇인가?
  15. S1A 2008 둘째날 SpringSecurity 2.5

Facebook comments:

to “스프링 3.1 (11) 심심풀이 @RequestMapping 요청 조건 퀴즈 풀이”

  1. You really make it seem really easy along with your presentation however I find this topic to be really something which I think I might never understand.
    It seems too complex and very huge for me. I’m taking a look forward in your next
    submit, I will try to get the cling of it!

  2. 창의적인 크리에이티브를 위해 매일 머리를 싸매는 디자이너들에게 도움이 될 사이트를 정리해봤습니다.

  3. I would like to thank you for the efforts you’ve put
    in writing this blog. I’m hoping the same high-grade
    website post from you in the upcoming also. Actually your creative writing skills has inspired me to get my own site now.
    Actually the blogging is spreading its wings quickly.
    Your write up is a good example of it.

    Feel free to visit my site :: BioNatrol Keto Review

  4. best place to buy kratom online
    where to buy kratom locally
    opms liquid kratom
    kraken kratom
    red sunda kratom
    zilis cbd oil
    how does cbd make you feel
    cbd oil at walmart
    cbd store near me

  5. usually posts some quite intriguing stuff like this. If you are new to this site

  6. honest revenue currently viagra briefly being thin bathroom unfortunately
    [url=http://viacheapusa.com/]viagra[/url] physically cook item
    method http://viacheapusa.com/ left mall http://viacheapusa.com/

  7. I have read so many articles about the blogger lovers however this piece of writing is really a good piece
    of writing, keep it up.

  8. I was more than happy to find this site. I wanted to thank you for your time for this particularly fantastic read!!
    I definitely savored every bit of it and i also have you saved to
    fav to see new stuff in your website.

  9. You really make it appear so easy with your presentation but I to find this matter to be really one thing that I feel
    I would never understand. It seems too complex and very huge for me.
    I’m having a look ahead for your subsequent submit, I’ll
    try to get the hang of it!

  10. grain cialis generic best price negotiate
    fight [url=http://cialisle.com/]cialis generic best price[/url] breast
    shrug beginning http://cialisle.com/ glove http://cialisle.com/

  11. Hey I am so glad I found your blog page, I really found you by
    mistake, while I was searching on Askjeeve for something
    else, Anyhow I am here now and would just like to say kudos for a incredible
    post and a all round thrilling blog (I also love
    the theme/design), I don’t have time to read through it all
    at the minute but I have book-marked it and also added your RSS feeds,
    so when I have time I will be back to read much more,
    Please do keep up the superb job.

  12. Keep this going please, great job!

  13. Very good information. Lucky me I ran across your site by chance (stumbleupon).

    I’ve saved it for later!

    My site; 4k tv, Elvira,

  14. Asking questions are really pleasant thing if you are not understanding something completely, however this
    piece of writing provides pleasant understanding even.

  15. mit 45 kratom
    buy kratom
    live kratom plants
    kratom
    maeng da kratom for pain
    cbd benefits
    does gnc sell cbd oil
    cbd oil
    cbd oil drops

  16. I am really impressed along with your writing abilities as neatly
    as with the layout to your blog. Is this a paid topic or did you modify it your
    self? Anyway keep up the excellent quality writing, it is uncommon to look a
    great weblog like this one today..

  17. Hello, I would like to subscribe for this webpage to obtain most up-to-date updates, therefore where can i
    do it please help.

  18. It’s very simple to find out any topic on web as
    compared to textbooks, as I found this article at this web site.

  19. online canadian pharmacy http://canadianpharmacyonlinesrvh.com/ – best online pharmacy canada drug pharmacy п»їcanadian pharmacy best canadian online pharmacy

  20. Exceptional post but I was wondering if you could write a litte more on this subject?
    I’d be very grateful if you could elaborate a little bit more.
    Thank you!

  21. After examine a number of of the weblog posts in your website now, and I truly like your method of blogging. I bookmarked it to my bookmark website record and can be checking back soon. Pls try my site as well and let me know what you think.

  22. I got this web site from my buddy who informed me about this web site and at the moment this time I am visiting this web
    site and reading very informative articles or
    reviews at this time.

  23. i http://cialisles.com/ cialisles.com, without cialis generika berichte cialis online pharmacy and cialis usa.

  24. I think the admin of this website is truly working hard for his site, as here
    every data is quality based material.

  25. is trazodone a narcotic http://trazodoneud.com/ – is trazodone addictive trazodone dosage for sleep trazodone uses trazodone for sleep

  26. I think this is one of the most vital info for me. And i am
    glad reading your article. But wanna remark
    on some general things, The site style is great, the articles is really great : D.
    Good job, cheers

    Here is my blog :: พัดลมระบายความร้อน

  27. how long do kratom withdrawals last
    buy kratom
    phytoextractum kratom
    kratom capsules
    kratom plants
    charlotte’s web cbd oil
    cbd oil no thc
    benefits of cbd oil
    cbd oil and drug test

  28. Hello, after reading this remarkable piece of writing i am too happy to share my experience here with friends.

  29. GENJIPOKER Bandar Domino Gaple pkv games Poker duit aseli
    terpercaya
    GENJIPOKER yakni laman pkv games dengan pelayanan daftar pembuatan akun tidak dipungut bayaran membership yang mempunyai beraneka variasi games taruhan kartu online yang terdiri dari poker online dan domino gaple atau domino online

    Seluruh games ada dalam satu platform taruhan kartu online pkv
    games dengan 2 versi ialah desktop untuk pengguna perangkat windows/mac dapat login pkv games lewat web.
    Versi mobile untuk pengguna android dan iphone dapat men-download
    aplikasi pkv games dari website sah GENJIPOKER.

    Transaksi deposit setor dana dan withdraw tarik dana sudah tersedia 10 bank indonesia yang
    terdiri dari BCA, Mandiri, Danamon, Cimb Niaga,
    BNI, Permata, BRI, Panin, OCBC Nisp dan Maybank.
    Cuma minimal 10ribu rupiah anda telah dapat bermain seluruh permainan dan berkesempatan menang jackpot
    progresif dengan sempurna ratusan juta rupiah
    tiap harinya.

    GENJIPOKER sebagai penyedia taruhan kartu domino gaple pkvgames
    terpercaya dengan metode paling aman, fairplay dan terjamin tiap-tiap transaksi chips berjalan dengan lancar.
    Pengerjaan kencang cuma 1 menit per transaksi dengan customer service yang betul-betul tanggapan dan ramah dalam memberikan pelayanan selama 24 jam melewati livechat, whatsapp dan bbm.

    Temukan juga Bonus Turnover sebesar 0.5% dan Bonus referral sebesar 15%
    yang dibagikan seminggu sekali dan segera masuk ke akun pkv
    games yang dapat seketika di tarik dana menjadi duit beneran atau
    diwujudkan modal bermain domino qiu qiu untuk menerima profit yang lebih besar lagi.

  30. Hey! This is kind of off topic but I need some guidance from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure
    things out pretty fast. I’m thinking about making my own but I’m not sure where to start.
    Do you have any tips or suggestions? Thank you

  31. Contrary to popular belief, no gaming going as far back
    as the 1950s continues to be 100% clay. So the past records and the current physical conditions from the players and in many cases expertise in team strategy are incredibly essential to make predicaments that win no less than some mentionable amount.
    But remember, function of every tournament is to finish the overall game
    with an increase of chips than your opponents in the event the tournament finishes.

  32. sildenafil plants [url=https://viaqraonlinegen.com/]buy viagra
    no prescription[/url] sildenafil y amiodarona viagra online sildenafil accion farmacologica magnus sildenafil
    50 mg masticable https://viaqraonlinegen.com/ levitra vs sildenafil

  33. great submit, very informative. I ponder why the opposite
    specialists of this sector don’t realize this.
    You must continue your writing. I’m sure, you’ve a huge readers’ base already!

  34. citrato de sildenafil causa dependencia [url=http://viabsbuy.com]buy viagra
    online discount[/url] contraindicaciones de sildenafil 50 mg buy generic viagra
    sildenafil 100 contraindicaciones sildenafil citrate fass http://viabsbuy.com cual
    es la funcion de la pastilla sildenafil

  35. seroquel anxiety http://quetiapineseroquelik.com/ – seroquel anxiety quetiapine fumarate side effects quetiapine davis pdf quetiapine for sleep

  36. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a
    comment is added I get four e-mails with the same comment. Is there any way you can remove people from that service?

    Thank you!

  37. Why visitors still make use of to read news papers
    when in this technological globe everything is presented on net?

  38. Undeniably consider that which you said. Your favourite justification seemed
    to be at the net the simplest thing to remember of.
    I say to you, I certainly get annoyed even as folks consider worries that they plainly don’t
    recognise about. You managed to hit the nail upon the highest as
    neatly as outlined out the whole thing with no need
    side-effects , other people could take a signal. Will probably be back to get more.
    Thank you

  39. Hi there Dear, are you actually visiting this site regularly, if so afterward you
    will without doubt obtain pleasant know-how.

Leave a Reply to Felipe Larkin Cancel reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2017 Toby's Epril Suffusion theme by Sayontan Sinha