menteith
menteith

Reputation: 678

Fail to have HttpServletRequest with additional argument in a method in a controller

I cannot make my controller working when I add additional parameter when I have HttpServletRequest as one of its parameters. The following code compiles and gives no exceptions.

@POST
@Path("new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String create(HttpServletRequest request) {

}

However, when I add additional parameter, an exception is thrown.

@POST
@Path("new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String create(HttpServletRequest request,  ClientDto clientDto) {

}

Here's the exception:

[[FATAL] Method public java.lang.String CreditController.create(
javax.servlet.http.HttpServletRequest,dto.ClientDto) on resource class CreditController
contains multiple parameters with no annotation.
Unable to resolve the injection source.;

Upvotes: 1

Views: 243

Answers (1)

eigenharsha
eigenharsha

Reputation: 2231

As per suggestion of @GyroGearless:

It seems to missing @Context annotation, you can try with this like,

@POST
@Path("new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String create(@Context HttpServletRequest request, ClientDto clientDto) {

Upvotes: 1

Related Questions