代码之家  ›  专栏  ›  技术社区  ›  CTN

无法从webview javafx中的api访问数据

  •  0
  • CTN  · 技术社区  · 9 年前

    无法从api加载数据。仅显示html设计。

    我使用hibernate spring mongodb作为数据的后端。在前端angular2.当我从JavaFXWebView访问url时,它不显示数据。

    javafx代码:

    final WebView browser = new WebView();
                final WebEngine webEngine = browser.getEngine();
                webEngine.load("http://192.168.2.6:4200/");
                webEngine.setJavaScriptEnabled(true);
                browser.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                    public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                        System.out.println(browser.getEngine().getLoadWorker().exceptionProperty());
                    }
                });
                TrustManager trm = new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
    
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
    
                    }
    
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
                };
    
                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, new TrustManager[] { trm }, null);
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    

    hibernate spring api:

    @CrossOrigin
        @SuppressWarnings("unchecked")
        @RequestMapping(value = "/getById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ResponseBody
        public <T> T getById(@RequestParam(ERPUtils.ID_PARAM_FOR_API) String id,
                @RequestParam(ERPUtils.ENTITY_NAME_PARAM_FOR_API) String entityName) {
    
            T t = null;
    
            /* It returns rough object of passed id. */
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_ROUGH)) {
                Rough rough = erpService.getById(id, new Rough());
                t = (T) rough;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_LOT)) {
                t = (T) erpService.getById(id, new Lot());
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_CUT)) {
                Cut cut = erpService.getById(id, new Cut());
                t = (T) cut;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_PIECE)) {
                Piece piece = erpService.getById(id, new Piece());
                t = (T) piece;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_TAG)) {
                Tag tag = erpService.getById(id, new Tag());
                t = (T) tag;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_PLAN)) {
                Plan plan = erpService.getById(id, new Plan());
                t = (T) plan;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_SUBPLAN)) {
                SubPlan subPlan = erpService.getById(id, new SubPlan());
                t = (T) subPlan;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_USER)) {
                User user = erpService.getById(id, new User());
                user.setPassword(null);
                user.setEncryptedToken(null);
                t = (T) user;
    
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_WEIGHT_UNIT)) {
                WeightUnit weightUnit = erpService.getById(id, new WeightUnit());
                t = (T) weightUnit;
            }
    
            if (entityName.equalsIgnoreCase(ERPUtils.TABLE_NAME_FOR_PRICE_UNIT)) {
                PriceUnit priceUnit = erpService.getById(id, new PriceUnit());
                t = (T) priceUnit;
            }
    
            return (T) (t == null ? ERPUtils.NO_RECORD_FOUND : t);
        }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   CTN    9 年前

    最终得到了答案。

    只需在javafx中添加以下行。

    System.setProperty("sun.net.http.allowRestrictedHeaders", "true");