Home > OS >  Do i need @Produces when I declare type in function
Do i need @Produces when I declare type in function

Time:10-22

I learned that writing @Consumes and @Produces annotations on every endpoint is good programming. However, if I declare the type in the endpoint, do I still need the annotation?

What is good programming?

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/pdf") // Content type declared in annotation
@Path(GET_BILL_FOR_SYSTEM)
public Response getBillForTheSystem(@QueryParam(value = "year") Long year) {
    return Response.ok( billSheetService.getBillForTheSystem(year) )
            .type( "application/pdf" ) // Content type declared in response builder
            .header( "Content-Disposition", "attachment; filename=\"BillForTheSystem.pdf\"" )
            .build();
}

CodePudding user response:

You are correct. There is no functional reason to declare the value of the response content type twice. In fact, such duplication is often frowned upon.

However, if you have tooling that inspects your code to generate documentation, it might be able to read the annotation but not the type returned by the code itself. So in that case, I'd prefer leaving only the annotation.

  • Related