Second HTTP server with Spring Boot - spring

I'm using Spring Boot to create a rest server and everything is going fine. But, now I want to create a second http listener in another port to handle static content such as Html, css, js and etc. I've been reading the documentation but I didn't get it. How can I do it?

Static contents can be easily served if you place it static folder in resources.
https://dzone.com/articles/spring-boot-integrating-static-content
Is this what you are looking for?

Related

Blank Pages when accessing services through Spring Cloud Gateway

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)?

mimic swagger api on spring boot application

I got a Spring boot REST based proxy server application with standard REST End point exposed(GET, POST,PUT and DELETE) to outside world.
Any requests coming from external world then will be routed to actual functionality internally.
Now the requirement is to expose all the API supported by my REST proxy server. As I mentioned I do not have static controller path in my code for individual APIs. So would like to get your input how to create swagger support for all the internal APIs that my proxy server support. Is there are a way I can generate swagger schema manually to mimics the controller path? Or is there any other way to solve this problem?
Thanks in advance.

Use zuul to deliver static content

I have a Zuul reverse proxy in front of my spring boot microservices, used as an API gateway.
Can the Zuul spring boot app be used to deliver static content, IE client code that calls the API gateway? I dont want to host this in a microservices or have another VM to manage for hosting content
I tried having a static folder in resources folder, but cant figure out how to map this in the application.yml responsible for routing. I dont want to route it to a NGnix or apache server, but use the embedded Tomcat of Zuul Spring boot app.
What would be the route to put in the route config, or what is the best approach for this.
Thank you
You don't have to map it in the routes config. Whatever you put in the resources/static folder will be served, as long nothing else is mapped on the same path.

Spring Boot REST JSP

I have a working Spring BOOT application that has a custom security provider and REST API controllers. I would also like to add a GUI interface to the application for access from a browser through jsps, html, and a login page which uses my existing custom security provider I used with the REST APIs. Maybe using Spring MVC since that is needed for the REST API support. I could not find a single example of doing this on the web. Also, I do NOT want to use any web XML based configuration files - as I am currently only using Java config for the implementation of the REST APIs. I am also currently using SSL for REST API access through SSL in a Jetty embedded web container. Please help if you can? Thanks in advanced.
Paul there is a rather large amount of information on view technologies that are compatible with Spring BOOT. You need to decide what you want to use and do relevant research for it.
As a guiding hand here check this page out for just one of the many types:
http://kielczewski.eu/2014/04/spring-boot-mvc-application/
You may follow this procedure :
lets assume that u have an endpoint for which you need both REST and view controllers,your REST endpoint exposes your data in JSON as RESTController and your view Endpoint returns the view name as Simple old controller.
lets say your base url is at localhost:8080 and your endpoint of interest is /students
you could have both in same application but at different endpoints like this :
REST : localhost:8080/api/v1/students -- exposes json
VIEW : localhost:8080/students -- returns a view
hope this make clear ..

Spring MVC and Web Application separated

I have been Googling a lot lately, but I find myself coming up short on answers.
I have a complete Spring MVC application secured by Spring Security and with services exposing logic to controllers (controllers -> service -> repository -> POJO's).
Currently all my controllers, except the login controller, serve nothing but JSON/XML and I want it to stay this way. I do not want to render different views on each controller.
However, what I want to be able to, is to separate the web application from the backend because in time I want to be able to integration with my service using more platforms than just a web browser.
Currently I have the regular Spring MVC application:
Configuration
Domain(POJO's)
Repository
Service
Controller
Login is done using a Thymeleaf rendered view and Spring Security which does nothing more than filtering all urls under the application root. After this, it is just a bunch of static files being served as resources:
Spring Controllers send a "{ontrollerName}/layout" to serve the AngularJS HTML partial used for all data under that given Spring Controller.
What I want, is a way to separate everything in the /webapp directory from the rest of my project. I have looked into a few different solutions here such as using Apache Proxy where my Apache Http server hosts the client code which communicate with the backend using Ajax to rest controllers, and Tomcat hosting the backend. However, I hear that proxying like this is not safe. I could however be dead wrong here.
So, the questions I hope to receive answers to are:
1. Is it fine to just write a client that uses Apache http server's Proxy to provide access to Ajax on the server?
If not; How should I proceed? Is it any point in trying to extract the client side from the /webapp directory or is this just some stupid idea I created because it seemed cool to be able to deploy them without having to relay on each others?
Is there any best practices in regards to how I structure a project with separate modules? Think Gradle build scripts for multi modules.
Should I think in different terms and use an approach not based on Spring MVC at all? If so, please advice me.
I hope this was clear enough to make sense for you guys.
The solution was to use the simpler approach of a restful API that did authentication over OAuth 2.
Basically the two best alternatives are a hybrid solution (part restful API and part server side rendering of pages) or a full blown restful API where you implement all functionality in a static web client. This way there is no need for multi-module projects and packing things together in one package.

Resources