我正在研究球衣框架。我对这篇文章很困惑,第一节是json - >杰克逊。 https://jersey.java.net/documentation/latest/media.html
我不知道他为什么扩展ObjectMapper并注册这个MyObjectMapper,为什么不直接注册提供者呢? ObjectMapper和提供者之间的关系是什么?
我试着直接注册一个jackson提供程序,但是当我运行启动应用程序时,我得到"堆栈溢出错误",因为我得到无限循环。
public class App {
private static URI BASE_URI = URI.create("http://localhost:8080/");
public static void main( String[] args ) {
System.out.println( "Hello World!" );
try {
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new MyBlogApplication(), false);
server.start();
} catch (IOException ie) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ie);
}
}}
class MyBlogApplication extends ResourceConfig {
public MyBlogApplication() {
packages("org.xiongbingchao.resources;org.xiongbingchao.service");
register(JacksonJsonProvider.class);
}
}
我的宁静资源:
@Path("/articles")
public class ArticlesResource {
@GET
@Produces("application/json")
public Vector<Article> getArticleList() {
return new Vector<Article>();
}
@GET
@Path("/{articleId}")
@Produces("application/json")
public Article getArticle(@PathParam("articleId") int articleId) {
return new Article();
}
}
当我访问localhost时:8080 / articles / 3
错误堆栈: 注意:顶部内容有很多重复,所以我隐藏它们
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:92)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:711)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:434)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:329)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:384)
at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:224)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.xiongbingchao.dao.Article
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690)
Caused by: java.lang.StackOverflowError
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:709)
... 1011 more
在浏览器中,我获得了许多重复记录:
{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":{"article_id":0,"user_id":0,"content":"","created_at":"","updated_at":"","articleById":
2016年8月1日
添加我的代码:
public class Article {
public int article_id;
public int user_id;
public String content;
public String created_at;
public String updated_at;
public Article () {
this.article_id = 0;
this.user_id = 0;
this.content = "";
this.created_at = "";
this.updated_at = "";
}
public Article getArticleById() {
return new Article();
}
public Vector<Article> getArticleList(Vector<Integer> articleIds) {
return new Vector<Article>();
}
}