How to set response header in JAX-RS so that user sees download popup for Excel?

后端 未结 3 448
慢半拍i
慢半拍i 2020-12-01 03:02

I wrote code that generate Excel file using REST JAX-RS and I confirmed that the generated Excel file is in GlassFish server directory.

But my goal is when user clic

3条回答
  •  眼角桃花
    2020-12-01 03:26

    You don't need HttpServletResponse to set a header on the response. You can do it using javax.ws.rs.core.Response. Just make your method to return Response instead of entity:

    return Response.ok(entity).header("Content-Disposition", "attachment; filename=\"" + fileName + "\"").build()
    

    If you still want to use HttpServletResponse you can get it either injected to one of the class fields, or using property, or to method parameter:

    @Path("/resource")
    class MyResource {
    
      // one way to get HttpServletResponse
      @Context
      private HttpServletResponse anotherServletResponse;
    
      // another way
      Response myMethod(@Context HttpServletResponse servletResponse) {
          // ... code
      }
    }
    

提交回复
热议问题