1、webx的請求URL和springboot/springmvc不同問題;
如下圖爲webx的URL的請求處理過程,springboot中的請求路徑通過RequestMapping,GetMapping,PostMapping等的註解顯示配置,並一目瞭然,顯示可見的,其中產生了兩個問題。
- 對應URL遷移到新springboot比較繁瑣,且web對與url的訪問支持多種方式,例如使用/shop/batch_buy和/shop/batchBuy都能匹配到Shop.java同一個方法,對遷移帶來很大困難。
- 對應的URL請求時可以攜帶action用於前期處理,springboot並不支持,升級需要提供其他方案。
a、關於url的升級問題沒有什麼好的方法,只能通過對應java類的路徑+方法,默認加入到springboot中,通過原有系統前端頁面中的請求測試新系統url。
b、對於同一個請求不同處理問題,目前可以通過兩種方案解決。如下圖第一個請求在springboot中可以配置:@RequestMapping(value="/mywebx/login.htm") ,以下以該配置作用在login()方法上爲例講述。
具體的參考:https://www.iteye.com/blog/tom-seed-2248096
webx中對應的Login.java代碼可以通過controller,但是對應的LoginController.java在springboot的流程中無法實現自動查找對應的前期處理,springboot升級必須滿足前期處理action邏輯,並實現可擴展。
升級方案一:將LoginController.java提出一個接口,擴展的action作爲一個組件並都實現該接口,並使用loginController作爲組件名稱,在對應的login()方法最頂部通過策略模式調用組件方法.
升級方案二:使用多個方法,請求的ULR相同但增加params配置,具體如下
a、@RequestMapping(value="/mywebx/login.htm") 在方法中不含LoginController代碼實現。
b、@RequestMapping(value="/mywebx/login.htm",params="action=LoginController") 在方法中包含LoginController代碼實現。
2、webx的webx-channel.xml對應的默認配置
webx系統在URL無法匹配到對應的controller時,依然可以通過默認的處理方案,webx-channel.xml 中配置的流程就是實現該功能的。該流程可以返回json數據,也可以獲取遠程頁面資源在本地吐出。
springboot中並不存在默認的URL處理器,但springboot可以通過容器獲取到所有配置的url,所有突破口在這裏,以獲取外部頁面資源爲例,具體實現如下。
1、實現一個ChannelController 用於處理默認的處理邏輯 代碼可以如下 url =/channel 後續需要用到
@Controller
@Slf4j
public class ChannelController {
@Autowired
HttpServletRequest httpRequest ;
@RequestMapping(value="/channel")
public ResponseEntity<byte[]> getResource(Stirng url) {
byte[] content = "獲取遠程url對應的vm,HTML,或者jsp資源";
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_HTML);
return new ResponseEntity<>(content, headers, HttpStatus.OK);
}
2、實現一個filter 用於匹配請求過來的URL代碼如下
@Component
@Order(2)
public class ResourceUrlFilter implements Filter{
@Autowired
private RequestMappingHandlerMapping mapping;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest)request;
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
boolean isMatch=false;
for (RequestMappingInfo info : map.keySet()) {
// 獲取url的Set集合,一個方法可能對應多個url
if(info.getPatternsCondition().getMatchingCondition(httpRequest)!=null) {
isMatch=true;
break ;
}
}
//根據請求參數獲取對應的外部Url
String url ="根據請求參數獲取對應的外部Url";
if(!isMatch) {
httpRequest.getRequestDispatcher("/channel?url="url).forward(request,response);
return ;
}
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}