I have three micro-services
Proxy service - spring boot app for auth verification and requesting nginx app to serve requests
UI : angular app hosted on tomcat
nginx as reverse proxy with other service endpoints and UI
When user makes request from browser, It first comes to proxy and then proxy sends response back by making rest call to nginx.
I can serve api calls from UI via proxy service by using rest template calls to nginx. However when UI is requesting static content then UI receives corrupted resource.
resttemplate response entity to nginx is string object, I also tried using byte[] for just static content but it did not help.
resttemplate call looks like this:
ResponseEntity<String> restResponse = restTemplate.exchange(uri, method, httpEntity, String.class);
appreciate quick help here.
Related
As a continuation of my learning of the Spring Cloud Gateway, I have created a small microservice (from Spring Boot) that serves web pages. This microservice is simple enough: it uses an application.properties file that contains the following:
server.port=8091
spring.resources.static-locations=classpath:/public/
The microservice has an index page in the resources folder, along with some style and other resources:
resources/public/index.html
resources/public/css/styling.css
resources/public/graphics/<pretty graphics files>
The Spring Cloud Gateway application uses a fairly straightforward routing configuration:
#Configuration
public class RouteConfig
{
#Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder routeLocatorBuilder)
{
return routeLocatorBuilder.routes()
.route("webdisplay",route-> route.path("/webdisplay/**").uri("localhost:8091"))
.build();
}
}
The Spring Cloud Gateway application listens on Port 8080.
When I access the simple microservice diectly:
http://localhost:8091
The index.html page is displayed as normal. Unfortunately, when I access the microservice through the Gateway:
http://localhost:8080/webdisplay
I get a blank page!
The page is blank, and the page source has nothing )(no HTML, nothing!). When using my browser's network debugger, I am seeing a response code of 200 with headers showing that HTML and other related web content is accepted. The HTML, however, is not actually getting to the browser.
Assuming that I am missing something, I went looking for examples of how to get HTML content through a Spring Cloud Gateway application. There are plenty of examples of how to get lists and text from toy microservices, and examples of how to serve static pages from a Spring Cloud Gateway application, but it appears that no one has used Spring Cloud Gateway to access webpages served by other applications! at least, no one has put up an example of doing so.
So if I am missing something that needs to be done, I haven't found it. Or perhaps this is a bug in the Gateway that needs to be reported somewhere?
Can anyone help with this? Does anyone know how to get Spring Cloud Gateway to work correctly with microservices that serve actual web content (static or dynamic)?
I have the following services
Service A - Sends request to service B using RestTemplate.
Service B - Has an end point that returns "Hello Universe".
Service G - "Spring Cloud Gateway".
Service A sends request to Service B using API Gateway URL using REST Template.
But, how does it work with Feign Client? For example below is the FeignClient of Service B in Service A
#FeignClient(name = "serviceb")
public interface ServiceBClient {
#GetMapping("/getmessage/")
public Inventory getInventoryDetails();
}
I believe one of the primary reasons we use a gateway is to allow the service A to send request to Gateway instead of directly using the service name.
But it seems with feign client the purpose is destroyed.
How to send request to Gateway using feign client?
if you want to give the URL of API-gateway, you can provide it instead of the service name. then the request doesn't go to the related service and, the request goes to the API gateway and, the API gateway will route the request to the relevant service. then the API gateway will be busy for nothing. because, if you gave the service name, the request directly goes to the request by using the cached services IP address data. due to the internal request, the request doesn't want to go through the security filters as well.
#FeignClient(url = "http://localhost:8081/order-service")
I have a Spring Rest web application and I want to invoke some json rest services from .net web application in IIS server. I used Jackson for rest. Is the any plugin or jar to invoke a web service from another server web service on same machine this task without getting connection URL and processing?
You can use Spring's RestTemplate to call ReSTFul API like below.
RestTemplate restTemplate = new RestTemplate();
POJO obj = restTemplate.getForObject("http://apiserver:8080/api/pojo", POJO.class);
log.info(obj.toString());
For more information visit official spring site.
I have a rest web service that is implemented using spring boot starter web. This service acts as a client to another application that requires authentication to make calls to it.
Calls made from the client to the server are using org.springframework.web.client.RestTemplate.
Is there a way to come up with a solution to add authentication headers to outbound requests at one single point before they are sent out?
I don't want to add headers in each of the requests separately.
Javadoc for RestTemplate says:
This template uses a SimpleClientHttpRequestFactory and a
DefaultResponseErrorHandler as default strategies for creating HTTP
connections or handling HTTP errors, respectively. These defaults can
be overridden through
HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory)
So I would take SimpleClientHttpRequestFactory and override its prepareConnection(..) method.
I am trying to write a very simple REST client using RestTemplate and Jackson JSON.
Tried such sample from spring.io Getting error 401 Authorization Required even on localhost or even if I include it into the same Tomcat6 Eclipse project where that REST WebService is running.
We don't require any authorization on that Web Service while in Dev or local environment,
and anyway I am an authorized user.
Is that something enforced by Spring RestTemplate?
Running client as Java application on Java 1.6.
I am new to REST and JSON.
No there is no enforced Authorization required by Spring's RestTemplate. You should be able to build or launch an example simple REST webservice and send requests to it with RestTemplate without receiving this response, so either the server you are sending the requests to or something inbetween is returning this response code. Can you share any more details of the service you are communicating with? Is it something internal or a public API which may require Authorization?