Comments (6)
In you can user readers and writers to achieve this, and there are also other options how to de-serialize/serialize a request.
For instance:
Read the request - serialize it to an object
@Consumes("application/json")
public class DummyReader implements ValueReader<Dummy> {
@Override
public T read(String value, Class<Dummy> type) {
if (StringUtils.isNullOrEmptyTrimmed(value)) {
return null;
}
return Dummy(value);
}
}
the REST
@POST
@Path("/dummy")
@RequestReader(DummyReader.class)
@ResponseWriter(DummyWriter.class)
public Dummy getDummy(Dummy dummy) {
return dummy;
}
Output the response
// now to convert Dummy to response
@Produces("application/xml") // optional content-type header
@Header("X-Status: I'm a dummy") // optional additional static headers
public class DummyWriter implements HttpResponseWriter<Dummy> {
@Override
public void write(Dummy data, HttpServerRequest request, HttpServerResponse response) {
response.status(200); // only needed when overriding 200
String out = data.name + "=" + data.value;
response.end("<custom>" + out + "</custom>");
}
}
Please read up in the documentation how to bind readers and writers correctly and how to handle exceptions.
from rest.vertx.
How do I get the path param, query param or header information in ValueReader?
from rest.vertx.
Please read the documentation ...
you can either use the @PathParam or the @QueryParam annotations and there are a lot of additional options to deserialize the request.
Alternatively you can "join" the request and deserialize with @BeanParam ..
Depends on you use case.
from rest.vertx.
I read then document many times...., because they cannot satisfy my requirement.
Because I want to use single reader handler to do uniform thing, then I can apply it in specific route path.
For example
@interceptor(AuditLog.class)
@get(value = "/getSADeviceById", produces = "application/json")
ApiResponse getDevice(@context context)
Currently, I used to do by guice interceptor(AuditLog.java)
I need to print the audit log in the route path, which will read the context information in method(getDevice), and print route information, parameters, headers.
If like you said, I will do a lot of duplicate thing for different route path.
ApiResponse getDevice1(@Header header, @PathParam("xxxxx") paramter, @context context) {
// duplicate code
System.out.println(header);
System.out.println(paramter);
}
ApiResponse getDevice2(@Header header, @PathParam("xxxxx") paramter, @context context) {
// duplicate code
System.out.println(header);
System.out.println(paramter);
}
ApiResponse getDevice3(@Header header, @PathParam("xxxxx") paramter, @context context) {
// duplicate code
System.out.println(header);
System.out.println(paramter);
}
But if there is interceptor, I only focus on my logic code.
@interceptor(AuditLog.class)
@get(value = "/getSADeviceById1", produces = "application/json")
ApiResponse getDevice1(@context context) {
}
@interceptor(AuditLog.class)
@get(value = "/getSADeviceById2", produces = "application/json")
ApiResponse getDevice2(@context context) {
}
@interceptor(AuditLog.class)
@get(value = "/getSADeviceById3", produces = "application/json")
ApiResponse getDevice3(@context context) {
}
from rest.vertx.
If you only want to log/inspect your request before it hits the api handler you can simply add a handler before registering RESTs.
For instance:
Router router = Router.router(vertx);
// The log handler
router.route().handler(LoggerHandler.create(LoggerFormat.DEFAULT));
// Rest vert.x registration
RestRouter.register(router, MyRest.class);
Read up on the LoggerHandler provided by vertx to alter to your own taste.
Hope this helps.
from rest.vertx.
But this solution is not very convenient, if could support annotation, it will be more helpful. So If you could put the RoutingContext (Or HttpServerRequest) in ValueReader or RequestReader like ResponseWriter, it will be very helpful, but currently, there is only string type.
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
- Registered ValueReader not used to decode query parameters unless explicitly stated
- 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
- 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.