Java Code Examples for org.springframework.cloud.gateway.route.RouteDefinition#setFilters()
The following examples show how to use
org.springframework.cloud.gateway.route.RouteDefinition#setFilters() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatewayRouteService.java From SpringCloud with Apache License 2.0 | 6 votes |
/** * 将数据库中json对象转换为网关需要的RouteDefinition对象 * * @param gatewayRoute * @return RouteDefinition */ private RouteDefinition gatewayRouteToRouteDefinition(GatewayRoute gatewayRoute) { RouteDefinition routeDefinition = new RouteDefinition(); routeDefinition.setId(gatewayRoute.getRouteId()); routeDefinition.setOrder(gatewayRoute.getOrders()); routeDefinition.setUri(URI.create(gatewayRoute.getUri())); ObjectMapper objectMapper = new ObjectMapper(); try { routeDefinition.setFilters(objectMapper.readValue(gatewayRoute.getFilters(), new TypeReference<List<FilterDefinition>>() { })); routeDefinition.setPredicates(objectMapper.readValue(gatewayRoute.getPredicates(), new TypeReference<List<PredicateDefinition>>() { })); } catch (IOException e) { log.error("网关路由对象转换失败", e); } return routeDefinition; }
Example 2
Source File: GatewayControllerEndpointTests.java From spring-cloud-gateway with Apache License 2.0 | 6 votes |
@Test public void testPostRouteWithNotExistingFilter() { RouteDefinition testRouteDefinition = new RouteDefinition(); testRouteDefinition.setUri(URI.create("http://example.org")); FilterDefinition filterDefinition = new FilterDefinition( "NotExistingFilter=test-config"); testRouteDefinition.setFilters(Collections.singletonList(filterDefinition)); testClient.post() .uri("http://localhost:" + port + "/actuator/gateway/routes/test-route") .accept(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(testRouteDefinition)).exchange() .expectStatus().isBadRequest(); }
Example 3
Source File: GatewayUtils.java From jeecg-cloud with Apache License 2.0 | 5 votes |
/** * redis中的信息需要处理下 转成RouteDefinition对象 * - id: login * uri: lb://cloud-jeecg-system * predicates: * - Path=/jeecg-boot/sys/**, * @param array * @return */ public static List<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException { List<RouteDefinition> ls = new ArrayList<>(); for(int i=0;i<array.size();i++) { JSONObject obj = array.getJSONObject(i); RouteDefinition route = new RouteDefinition(); route.setId(obj.getString("id")); Object uri = obj.get("uri"); if(uri==null){ route.setUri(new URI("lb://"+obj.getString("name"))); }else{ route.setUri(new URI(obj.getString("uri"))); } Object predicates = obj.get("predicates"); if (predicates != null) { JSONArray predicatesArray = JSONArray.parseArray(predicates.toString()); List<PredicateDefinition> predicateDefinitionList = predicatesArray.toJavaList(PredicateDefinition.class); route.setPredicates(predicateDefinitionList); } Object filters = obj.get("filters"); if (filters != null) { JSONArray filtersArray = JSONArray.parseArray(filters.toString()); List<FilterDefinition> filterDefinitionList = filtersArray.toJavaList(FilterDefinition.class); route.setFilters(filterDefinitionList); } ls.add(route); } return ls; }
Example 4
Source File: GatewayControllerEndpointTests.java From spring-cloud-gateway with Apache License 2.0 | 5 votes |
@Test public void testPostValidRouteDefinition() { RouteDefinition testRouteDefinition = new RouteDefinition(); testRouteDefinition.setUri(URI.create("http://example.org")); FilterDefinition prefixPathFilterDefinition = new FilterDefinition( "PrefixPath=/test-path"); FilterDefinition redirectToFilterDefinition = new FilterDefinition( "RemoveResponseHeader=Sensitive-Header"); FilterDefinition testFilterDefinition = new FilterDefinition("TestFilter"); testRouteDefinition.setFilters(Arrays.asList(prefixPathFilterDefinition, redirectToFilterDefinition, testFilterDefinition)); PredicateDefinition hostRoutePredicateDefinition = new PredicateDefinition( "Host=myhost.org"); PredicateDefinition methodRoutePredicateDefinition = new PredicateDefinition( "Method=GET"); PredicateDefinition testPredicateDefinition = new PredicateDefinition( "Test=value"); testRouteDefinition.setPredicates(Arrays.asList(hostRoutePredicateDefinition, methodRoutePredicateDefinition, testPredicateDefinition)); testClient.post() .uri("http://localhost:" + port + "/actuator/gateway/routes/test-route") .accept(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(testRouteDefinition)).exchange() .expectStatus().isCreated(); }
Example 5
Source File: DynamicRouteConfigurationService.java From wecube-platform with Apache License 2.0 | 4 votes |
protected void buildRouteDefinition(String context, DynamicRouteItemInfo itemInfo) { RouteDefinition rd = new RouteDefinition(); rd.setId(context + ROUTE_ID_SUFFIX); String urlStr = String.format("%s://%s:%s", itemInfo.getHttpScheme(), itemInfo.getHost(), itemInfo.getPort()); URI uri = UriComponentsBuilder.fromHttpUrl(urlStr).build().toUri(); rd.setUri(uri); PredicateDefinition pd = new PredicateDefinition(); pd.setName("Path"); Map<String, String> predicateParams = new HashMap<>(8); predicateParams.put("pattern", String.format("/%s/**", context)); pd.setArgs(predicateParams); rd.setPredicates(Arrays.asList(pd)); List<FilterDefinition> filters = new ArrayList<>(); FilterDefinition fdDynamicRoute = new FilterDefinition(); fdDynamicRoute.setName("DynamicRoute"); fdDynamicRoute.addArg("enabled", "true"); filters.add(fdDynamicRoute); if (dynamicRouteProperties.isEnableRetry()) { FilterDefinition fdRetry = new FilterDefinition(); fdRetry.setName("ExRetry"); fdRetry.addArg("retries", "10"); fdRetry.addArg("series", "SERVER_ERROR"); // fdRetry.addArg("statuses", "NOT_FOUND"); fdRetry.addArg("methods", "GET,POST,PUT,DELETE"); fdRetry.addArg("exceptions", "java.io.IOException,java.net.ConnectException"); filters.add(fdRetry); } rd.setFilters(filters); add(rd); log.debug("### route added:{} {} {}", itemInfo.getContext(), itemInfo.getHost(), itemInfo.getPort()); }