Anar Sultanov
Anar Sultanov

Reputation: 3416

Rapidoid. How to return response from lambda without rendering?

I need to return a response from lambda without additional rendering. I tried to do it like this:

On.post("/locations/{id:\\d+}").serve((Integer id, Req req, Resp resp) -> {
    if (!storageService.locationIsPresent(id)) {
        return resp.code(404).json("Location not found!");
    }
    try {
        String request = new String(req.body());
        Location location = mapper.readValue(request, Location.class);
        storageService.updateLocation(id, location.getCountry(), location.getCity(), location.getPlace(), location.getDistance() );
    } catch (Exception ex) {
        return resp.code(400).json("Bad request!");
    }
    return resp.code(200).body(EMPTY_RESP);
});

But as a result I am getting an exception:

ERROR | 20/Aug/2017 14:19:53:460 | executor2 | org.rapidoid.http.impl.lowlevel.LowLevelHttpIO | Error occurred when handling request! | error = java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.rapidoid.http.impl.RespImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.rapidoid.http.impl.RespImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

Please tell me how to do it correctly.

And I would also like to know how to configure the server for a faster response with a large number of requests per second. Since in my case it is very slow.

Thank you in advance!

Upvotes: 1

Views: 472

Answers (2)

Anar Sultanov
Anar Sultanov

Reputation: 3416

Came across my old question and decided to share the solution. To return the pre-serialized JSON, it's possible to do like this:

On.get("/").serve((Integer id, Req req, Resp resp) -> {
   byte[] hardcodedJSON = "{\"x\": 123}".getBytes();
   return resp.contentType(MediaType.JSON).body(hardcodedJSON);
});

Upvotes: 1

Imran
Imran

Reputation: 1902

Try to configure your mapper like the following, ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); Here is some info on the problem, http://www.baeldung.com/jackson-exception

Upvotes: 1

Related Questions