micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Can a RequestBean access Body ?

Open Solido opened this issue 5 years ago • 7 comments

Hello, congratulation for 2.0 release !

Expected Behaviour

RequestBean can access any information like @QueryValue I'd like to access @Body

Actual Behaviour

@Body annotation can't be added to field

Thank you, Robert

Solido avatar Jun 27 '20 13:06 Solido

@Solido Please upload an example project with steps to reproduce the issue and details on what the error is or what behavior you are seeing

jameskleeh avatar Jun 29 '20 15:06 jameskleeh

Hi !

I want to avoid having to use @RequestBean as one parameter and @Body as another CompletableFuture bounds(@Valid @RequestBean Positionable render, @Body String body)

Currently @Body annotation is not supported as field marker so this code does not compile `@Introspected class Pt {

// THIS IS NOT WORKING
@Body String body

@NotNull
@PathVariable
String vid

@QueryValue
@NotNull
double dx

@QueryValue
@NotNull
double dy

}`

Thanks !

Solido avatar Jul 01 '20 22:07 Solido

This is a little bit more complicated because other areas search for a body argument and make decisions about how it will be processed.

jameskleeh avatar Jul 07 '20 23:07 jameskleeh

I know it's not trivial yet you see the problem having to search the request at two differents places.We'll refactor later if it land.

Thank you @jameskleeh !

Solido avatar Jul 09 '20 18:07 Solido

Would be nice to have this soon.

Our use case is that our app has two protocols/interfaces:

  • One of them is HTTP, which is handled by a controller.
  • The other one is via Kafka events, so we are receiving HTTP information in those events (like method, path, query parameters, headers, body, etc) and we convert it into the @RequestBean POJO to call the controller layer, as we don't want to duplicate the logic. Right now we have to map this Kafka event to two separate objects and then call the controller, but it can be simplified if @Body annotation is applicable to a request bean field.

mcanalesmayo avatar Dec 04 '20 01:12 mcanalesmayo

Hi all,

What about this?

As workaround if you place the @Body annotation before @RequestBean it works. I think its a question of order.

volnei avatar May 25 '22 18:05 volnei

Update on this: 2024

If the @Body annotation is placed before the @RequestBean annotation, it takes precedence, which means any fields annotated with @field (such as @field:PathVariable or @field:Query) will not be considered.

@Serdeable(naming = SnakeCaseStrategy::class)
data class Demo {
    @field:PathVariable("mark_id")
    val markId: String?,
    val operation: String?,
    val timeToLive: Int?
}

In the example above, all fields except mark_id will be populated.

Conversely, if you place @RequestBean before @Body, the annotations will work correctly, but you must write the request body field names in snake case. Attempts to use @JsonProperty or @field:JsonProperty to modify this behavior are ineffective.

@Serdeable(naming = SnakeCaseStrategy::class)
data class Demo {
    @field:PathVariable("mark_id")
    val markId: String?,
    val operation: String?,
    val time_to_live: Int?
}

Thapamanish avatar Aug 13 '24 07:08 Thapamanish