How can I redirect all unknown URLs in lift framework?

╄→尐↘猪︶ㄣ 提交于 2020-01-03 04:46:13

问题


I'm not very familiar with lift framework and wanted to know if the following use case is possible using lift framework.

On server1, Lift is serving REST webservice at following url "/contact/"

However, if the client sends request to the following URL https://server1/contact/meet/" then it is not implemented on this specific server but "might" be implemented by another server. Can Lift redirect any such unsupported URLs to some specific server? Eg, in 302 response, can Location be specified by Lift to https://server2/contact/meet/ ?

Please note that these are unknown URLs and can't be configured statically.


回答1:


Yeah, I get it. Maybe you need LiftRules.dispatch and net.liftweb.http.DoRedirectResponse. Following is the code I try to solve your trouble.

// The code should in the server1; JsonDSL will be used by JsonResponse
class Boot extends Bootable with JsonDSL {
   def boot {
       initDispatch
   }


   def initDispatch {
      LiftRules.dispatch.append { 

        case Req("contact" :: url :: Nil, _, GetRequest) => {
            () => Full(
                if (url == "join") {
                    // or other url that match what will be implemented in server1

                    // your implementation, say JsonResponse
                    JsonResponse("server1" -> true)
                } else {
                    // if the url part does not match simply redirect to server2, 
                   // then you have to deal with how to process the url in server2

                    DoRedirectResponse("https://server2/contact/meet/")
                }
            )
        }
     }
   }
 }

Anyway, hope it helps.



来源:https://stackoverflow.com/questions/33747110/how-can-i-redirect-all-unknown-urls-in-lift-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!