javafile-uploadjerseymultipartform-datasecurity-context

Jersey I can't authorize the user when uploading files


In default functions I can use ContainerRequestContext requestCtx to get the securitycontext and username from the request.

But for some reason when using this in a function that consumes MediaType.MULTIPART_FORM_DATA I'm getting this error:

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.

Code:

@POST
@Path("{albumcode}")
@RolesAllowed("user")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadPicture(@PathParam("code") String code,
FormDataMultiPart multipart, 
ContainerRequestContext requestCtx) {

Without ContainerRequestContext requestCtx the code from above works just fine.

Whats the best way to get the data from my ContainerRequestContext and also being able to upload files at the same time?


Solution

  • Found the solution while reading through random posts. I found out that adding the @Context annotation infront of my ContainerRequestContext requestCtx solved the problem.

    I hope I can help someone else with this problem couldn't find any other answers close to this.

    @POST
    @Path("{albumcode}")
    @RolesAllowed("user")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadPicture(@PathParam("code") String code,
    FormDataMultiPart multipart, 
    @Context ContainerRequestContext requestCtx) {