Skip to content

Commit

Permalink
GH-5 NotFoundException: null
Browse files Browse the repository at this point in the history
Requesting service by direct URL fixed
  • Loading branch information
lex-em committed Mar 21, 2019
1 parent 8e22f88 commit 5d2e900
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ public class ZuulSpringfoxSwaggerConfiguration {
@EnableSwagger2
public static final class EnableSwagger2Config {}

@ConditionalOnMissingBean(RestTemplate.class)
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
public RestTemplate loadBalancedRestTemplate() {
return new RestTemplate();
}

@Bean
public RestTemplate pureRestTemplate() {
return new RestTemplate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import ru.reliabletech.zuul.swagger.exception.NotFoundException;
import ru.reliabletech.zuul.swagger.props.ServicesSwaggerInfo;

import java.util.Optional;

/**
* General implementation
*
* @author Alexandr Emelyanov <mr.lex91@gmail.com>
* on 27.11.2017.
*/
@Component
@Slf4j
public class GenericSwaggerService implements SwaggerService {

@Autowired
private RestTemplate restTemplate;
@Qualifier("pureRestTemplate")
private RestTemplate pureRestTemplate;
@Autowired
@Qualifier("loadBalancedRestTemplate")
private RestTemplate loadBalancedRestTemplate;
@Autowired
private ServicesSwaggerInfo servicesSwaggerInfo;
@Autowired
Expand All @@ -39,8 +48,9 @@ public ObjectNode getSwaggerDoc(String route) {

@Override
public ObjectNode getOriginalSwaggerDoc(String route) {
String serviceUrl = servicesSwaggerInfo.getServiceUrl(route)
.orElseGet(() -> servicesSwaggerInfo.getDefaultProtocol() + route);
Optional<String> serviceUrlOpt = servicesSwaggerInfo.getServiceUrl(route);
String serviceUrl = serviceUrlOpt.orElseGet(() -> servicesSwaggerInfo.getDefaultProtocol() + route);
RestTemplate restTemplate = serviceUrlOpt.map(x -> pureRestTemplate).orElse(loadBalancedRestTemplate);
String url = String.format("%s/%s",
serviceUrl,
servicesSwaggerInfo.getSwaggerUrl(route));
Expand All @@ -50,6 +60,7 @@ public ObjectNode getOriginalSwaggerDoc(String route) {
if (e.getMessage() == null || !e.getMessage().startsWith("No instances available for")) {
throw e;
}
log.error("Requested resources URL is wrong", e);
throw new NotFoundException();
}
}
Expand Down

0 comments on commit 5d2e900

Please sign in to comment.