Java 无法序列化批注?

Java 无法序列化批注?,java,rest,serialization,jersey,jackson,Java,Rest,Serialization,Jersey,Jackson,使用jersey序列化注释总是失败,并出现未知错误 最简单的例子是: 泽西资源 @TestAnno( id = "TestID" ) @Path( "/test" ) public class TestResource { @GET @Produces( MediaType.APPLICATION_JSON ) public TestAnno list() throws JsonGenerationException, JsonMappingException, IOExcep

使用jersey序列化注释总是失败,并出现未知错误

最简单的例子是:

泽西资源

@TestAnno( id = "TestID" )
@Path( "/test" )
public class TestResource
{
   @GET
   @Produces( MediaType.APPLICATION_JSON )
   public TestAnno list() throws JsonGenerationException, JsonMappingException, IOException
   {
      final TestAnno ta = getClass().getAnnotation( TestAnno.class );
      return ta;
   }
}
注释

@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.TYPE )
public @interface TestAnno
{
   @JsonProperty( "id" )
   String id();
}
电话总是回

HTTP ERROR: 500
Problem accessing /schema. Reason:
Server Error

调试日志为空,没有异常或有关错误的任何其他信息。jersey失败的原因是什么?

可能您没有此类序列化程序。考虑编写DTO类,并将注释中的所有信息映射到它。然后作为回应返回。 比如:

  public Response list() throws JsonGenerationException, JsonMappingException, IOException
   {
      final TestAnno ta = getClass().getAnnotation( TestAnno.class );
      MyDto dto = buildDto(ta);

      return Response.ok(dto);
   }

是的,这是一个选项,但是jackson应该能够在没有dto的情况下序列化这些对象。如果我通过
ObjectMapper mapper=new ObjectMapper()手动执行此操作;mapper.writeStringValue(ta),Jackson会序列化注释。那么触发Jackson direct和让Jersey触发有什么区别呢?我想这是关于一些映射器设置的,你可以在序列化上设置断点,看看那里发生了什么,也许可以稍微覆盖一下这个逻辑。现在我记不清确切的类名了,周日我在PC上工作时,我可以试着找出它。在问题解决之前,我已经实施了一个解决方法。
list()
的方法体更改为
ObjectMapper mapper=new ObjectMapper();返回mapper.writeStringValue(ta)。现在可以了。你在运行哪个杰克逊版本?我无法重现2.4.1中的问题。我似乎记得在处理旧版本的注释时有一个bug。