Comments (5)
Not quite sure what you have in mind ... can you illustrate a simple use case
from rest.vertx.
If you want to redirect a user to another external URL you have two options for doing this.
Option one is using HTTP redirect status codes which are 3xx codes. The limitation of this option is that you can't redirect to a URL with a different body (payload) and different content type.
Option two is using template engines to render an HTML page with Javascript and then redirect the user using Javascript with the customized request body and content type.
from rest.vertx.
I think what you are after is static file serving.
To achieve this you simply need to set a dedicated "file" writer to a REST endpoint ...
Rest.vertx already provides a default implementation out of the box, but you can create your own.
For instance the following endpoint:
@Path("docs")
public class ServeStaticFileRest {
@GET
@Path("/{path:.*}")
@ResponseWriter(FileResponseWriter.class)
public String serveDocFile(@PathParam("path") String path) {
return "some_folder/" + path;
}
}
Will return a "path" as a String. This path is then used by the FileResponseWriter to find and return the contents of the desired file from /resources
folder.
So for instance if you put in the /resources/some_folder/myfile.txt
the call to:
GET /docs/myfile.txt
will serve /resource/some_folder/myfile.txt
if present ... or return 404 not found.
from rest.vertx.
OK. What if we want dynamic file contents like passing variables to file writer to render the file?
from rest.vertx.
Then you will need to create your own writer / custom implementation ...
Just for illustration ... (NOT WORKING CODE):
@Path("docs")
public class ServeStaticFileRest {
@GET
@Path("/{path:.*}")
@ResponseWriter(MyFileResponseWriter.class)
public MyFileDesire serveDocFile(@PathParam("path") String path, @QueryParam("size") int size) {
return new MyFileDesire(path, size)
}
}
The writer would then look something like this:
public class MyFileResponseWriter implements HttpResponseWriter<MyFileDesire> {
@Context
RoutingContext context;
@Override
public void write(MyFileDesire desire, HttpServerRequest request, HttpServerResponse response) throws FileNotFoundException {
if (!fileExists(context, desire.path)) {
throw new FileNotFoundException(path);
}
// create custom output ... of your liking ...
MyFile file = getFromDesire(desire)
// write to output
response.putHeader("Content-Length", String.valueOf(file.size()));
response.write(Buffer.buffer(file.toByteArray()));
response.end();
}
protected boolean fileExists(RoutingContext context, String file) {
FileSystem fs = context.vertx().fileSystem();
return fs.existsBlocking(file);
}
}
from rest.vertx.
Related Issues (20)
- IllegalArgumentException when register ExceptionHandler HOT 6
- Authentication does not work as in example HOT 8
- Multipart requests support? HOT 4
- Response head already sent HOT 9
- NoContentResponseWriter does not return response HOT 1
- version 1.0.5 RouteDefinition BODY_HANDLER_PARAMS not include context HOT 1
- java 17 compatibility (TypeVariableImpl) HOT 4
- RestBuilder violates Vert.x 4.3's handler ordering HOT 3
- Is this possible to print the cause of exception? HOT 5
- Doesn't work with jakarta annonations HOT 3
- Improve not found handling HOT 1
- Add suspendable function support to asynchronous response types HOT 3
- Cannot use null enum value in REST endpoint url HOT 2
- Support Swagger or Open API HOT 1
- Rewrite authorization/authentication flow
- HttpMethod problem in vertx4 HOT 1
- The detailed exception is hidden when fail to getInstance HOT 2
- Could you add Interceptor in path route like spring? HOT 6
- mountSubRouter HOT 2
- About Interceptor HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rest.vertx.