简体   繁体   中英

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?

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) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM