Home:ALL Converter>Read placeholders in micronaut aspect

Read placeholders in micronaut aspect

Ask Time:2020-09-04T04:32:15         Author:cshapdev

Json Formatter

I want to create an aspect in micronaut to have a generic way to send an event to a queue with the information that is passed to some methods, as well as some information from the context e.g. method parameters, method response, duration, exceptions, request headers (for api methods) etc. However, I want to define in the annotation what data should be added to the event. I have a working example using spring boot and SpEL:

@Event( <- This is the annotation to create the event
     @Detail(key = "salary", value = "#request.salary.toString()"), <- This is the SpEL
     @Detail(key = "ipAddress", value = "#HttpServletRequest.getHeader('x-forwarded-for').split(',')[0]"), <- This gets the client ipAddress from the context
     @Detail(key = "userAgent", value = "#HttpServletRequest.getHeader('user-agent')"") <- This gets the browser

)
public void MyMethod(RequestInformation request){
}

In that example above the event is created only with 3 properties and send to the queue in json format

{
 details:[
    "salary":"6000",
    "ipAddress":"180.30.115.62",
    "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15"
 ]
}

I know that SpEL is not supported by Micronaut as it states in the documentation: Spring Expression Language (SpEL) - SpEL expressions are not supported, property placeholders are however

I've tried reading about property placeholders but most of the examples are related to getting values from .yml files.

I'd like to know if there is a way to do something similar without creating my own language in micronaut. Also, migrate to spring boot is not an option.

I'll thank any help, advice, or insight on how to achieve this task.

Author:cshapdev,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63731248/read-placeholders-in-micronaut-aspect
yy