【问题标题】:Jersey I can't authorize the user when uploading files泽西上传文件时我无法授权用户
【发布时间】:2017-11-16 14:09:13
【问题描述】:

在默认函数中,我可以使用ContainerRequestContext requestCtx 从请求中获取安全上下文和用户名。

但由于某种原因,在使用 MediaType.MULTIPART_FORM_DATA 的函数中使用它时,我收到了这个错误:

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

代码:

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

如果没有ContainerRequestContext requestCtx,上面的代码就可以正常工作。

从我的ContainerRequestContext 获取数据并同时上传文件的最佳方式是什么?

【问题讨论】:

    标签: java file-upload jersey multipartform-data security-context


    【解决方案1】:

    在阅读随机帖子时找到了解决方案。我发现在我的ContainerRequestContext requestCtx 前面添加@Context 注释解决了这个问题。

    我希望我能帮助解决这个问题的其他人找不到任何其他答案。

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

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 1970-01-01
      • 2015-03-07
      • 2012-07-20
      • 1970-01-01
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      相关资源
      最近更新 更多