Code Monkey home page Code Monkey logo

Comments (6)

drejc avatar drejc commented on June 12, 2024

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.

shuitai avatar shuitai commented on June 12, 2024

How do I get the path param, query param or header information in ValueReader?

from rest.vertx.

drejc avatar drejc commented on June 12, 2024

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.

shuitai avatar shuitai commented on June 12, 2024

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.

drejc avatar drejc commented on June 12, 2024

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.

shuitai avatar shuitai commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.