我在函数中声明类型时需要@Produces吗

Do i need @Produces when I declare type in function

我了解到在每个端点上编写 @Consumes@Produces 注释是很好的编程。但是,如果我在端点中声明类型,我还需要注解吗?

什么是好的编程?

@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();
}

你是对的。没有功能上的理由两次声明响应内容类型的值。事实上,这种重复往往是不受欢迎的。

但是,如果您有检查代码以生成文档的工具,它可能能够读取注释,但不能读取代码本身返回的类型。所以在那种情况下,我宁愿只留下注释。