The server encountered an internal error that prevented it from fulfilling this request.org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.party.dinner.entity.User.friends, could not initialize proxy - no Session (through reference chain: com.party.dinner.entity.User["friends"]); nested exception is org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.party.dinner.entity.User.friends, could not initialize proxy - no Session (through reference chain: com.party.dinner.entity.User["friends"])
    org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:204)
    org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:1037)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:995)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:944)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:441)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   root cause org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: com.party.dinner.entity.User.friends, could not initialize proxy - no Session (through reference chain: com.party.dinner.entity.User["friends"])
    org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
    org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
    org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
    org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
    org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
    org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
    org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
    org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1613)

转载:http://blog.csdn.net/nomousewch/article/details/8955796

jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解

  • @JsonIgnoreProperties

此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。

  • @JsonIgnore

此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。

  • @JsonFormat

此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")

  • @JsonSerialize

此注解用于属性或者getter方法上,用于在序列化时嵌入我们自定义的代码,比如序列化一个double时在其后面限制两位小数点。

  1. public class CustomDoubleSerialize extends JsonSerializer<Double> {
  2. private DecimalFormat df = new DecimalFormat("##.00");
  3. @Override
  4. public void serialize(Double value, JsonGenerator jgen,
  5. SerializerProvider provider) throws IOException,
  6. JsonProcessingException {
  7. jgen.writeString(df.format(value));
  8. }
  9. }
  • @JsonDeserialize

此注解用于属性或者setter方法上,用于在反序列化时可以嵌入我们自定义的代码,类似于上面的@JsonSerialize

  1. public class CustomDateDeserialize extends JsonDeserializer<Date> {
  2. private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. @Override
  4. public Date deserialize(JsonParser jp, DeserializationContext ctxt)
  5. throws IOException, JsonProcessingException {
  6. Date date = null;
  7. try {
  8. date = sdf.parse(jp.getText());
  9. } catch (ParseException e) {
  10. e.printStackTrace();
  11. }
  12. return date;
  13. }
  14. }
  • 完整例子
  1. //表示序列化时忽略的属性
  2. @JsonIgnoreProperties(value = { "word" })
  3. public class Person {
  4. private String name;
  5. private int age;
  6. private boolean sex;
  7. private Date birthday;
  8. private String word;
  9. private double salary;
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public int getAge() {
  17. return age;
  18. }
  19. public void setAge(int age) {
  20. this.age = age;
  21. }
  22. public boolean isSex() {
  23. return sex;
  24. }
  25. public void setSex(boolean sex) {
  26. this.sex = sex;
  27. }
  28. public Date getBirthday() {
  29. return birthday;
  30. }
  31. // 反序列化一个固定格式的Date
  32. @JsonDeserialize(using = CustomDateDeserialize.class)
  33. public void setBirthday(Date birthday) {
  34. this.birthday = birthday;
  35. }
  36. public String getWord() {
  37. return word;
  38. }
  39. public void setWord(String word) {
  40. this.word = word;
  41. }
  42. // 序列化指定格式的double格式
  43. @JsonSerialize(using = CustomDoubleSerialize.class)
  44. public double getSalary() {
  45. return salary;
  46. }
  47. public void setSalary(double salary) {
  48. this.salary = salary;
  49. }
  50. public Person(String name, int age) {
  51. this.name = name;
  52. this.age = age;
  53. }
  54. public Person(String name, int age, boolean sex, Date birthday,
  55. String word, double salary) {
  56. super();
  57. this.name = name;
  58. this.age = age;
  59. this.sex = sex;
  60. this.birthday = birthday;
  61. this.word = word;
  62. this.salary = salary;
  63. }
  64. public Person() {
  65. }
  66. @Override
  67. public String toString() {
  68. return "Person [name=" + name + ", age=" + age + ", sex=" + sex
  69. + ", birthday=" + birthday + ", word=" + word + ", salary="
  70. + salary + "]";
  71. }
  72. }
    1. public class Demo {
    2. public static void main(String[] args) {
    3. writeJsonObject();
    4. // readJsonObject();
    5. }
    6. // 直接写入一个对象(所谓序列化)
    7. public static void writeJsonObject() {
    8. ObjectMapper mapper = new ObjectMapper();
    9. Person person = new Person("nomouse", 25, true, new Date(), "程序员",
    10. 2500.0);
    11. try {
    12. mapper.writeValue(new File("c:/person.json"), person);
    13. } catch (JsonGenerationException e) {
    14. e.printStackTrace();
    15. } catch (JsonMappingException e) {
    16. e.printStackTrace();
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. // 直接将一个json转化为对象(所谓反序列化)
    22. public static void readJsonObject() {
    23. ObjectMapper mapper = new ObjectMapper();
    24. try {
    25. Person person = mapper.readValue(new File("c:/person.json"),
    26. Person.class);
    27. System.out.println(person.toString());
    28. } catch (JsonParseException e) {
    29. e.printStackTrace();
    30. } catch (JsonMappingException e) {
    31. e.printStackTrace();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. }
    37. 如果Entity中的延迟加载对象必须有值,可以用Hibernate.initialize把延迟加载的对象提前获取
    38. Hibernate.initialize( Entity.getXX() );

[JavaEE] Entity中Lazy Load的属性序列化JSON时报错的更多相关文章

  1. Vue+Typescript中在Vue上挂载axios使用时报错

    Vue+Typescript中在Vue上挂载axios使用时报错 在vue项目开发过程中,为了方便在各个组件中调用axios,我们通常会在入口文件将axios挂载到vue原型身上,如下: main.t ...

  2. mvc EF框架中,加载外键对象序列化对象时报错 序列化类型为XX的对象时检测到循环引用

    Newtonsoft.Json.dll 或者通过->工具->库程序包管理工具->NuGet管理包->联机 输入Newtonsoft或者json.net Newtonsoft.J ...

  3. vue2.0动态绑定图片src属性值初始化时报错

    在vue2.0中,经常会使用类似这样的语法 v-bind:src = " imgUrl "(缩写 :src = " imgUrl "),看一个案例 <te ...

  4. fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string: "13:02:19"

    问题:time类型数据插入不进mysql数据库:调试的时候报如下错误: Caused by: java.lang.NumberFormatException: For input string: &q ...

  5. Spring中的一个错误:使用Resources时报错(The annotation @Resources is disallowed for this location)

    在学习Spring的过程中遇到一个错误:在使用注解@resources的时候提示:The annotation @Resources is disallowed for this location 后 ...

  6. 在Ubuntu中,用mvn打包hadoop源代码时报错,正在解决中!!!

    报错信息如下: (各种配置在最后面) hadoop@administrator-virtual-machine:~/Downloads/tar/hadoop-3.0.0-alpha1-src$ mvn ...

  7. IDEA中执行maven命令:mvn clean 时报错

    问题描述: 完成项目中的功能后,想要git一下,就用maven命令先清除一下编译文件,紧接着系统报错 Error executing Maven. 2 problems were encountere ...

  8. pytorch中使用多显卡训练以及训练时报错:expect more than 1 value per channel when training, got input size..

    pytorch在训练中使用多卡: conf.device = torch.device('cuda:0' if torch.cuda.is_available() else "cpu&quo ...

  9. 踩坑,vue项目中,main.js引入scss文件时报错

    当我们在src目录下创建.scss文件,并在main.js中引用,运行时会报: ERROR Failed to compile with 1 errors 5:25:07 PMThis relativ ...

随机推荐

  1. c++ 静态持续变量

    c++为静态存储持续性变量提供了3种链接性: 外部链接性(可在其他文件中访问) 内部链接性(只能在当前文件中访问) 无链接性(别有用心能在当前函数或代码中访问) 如果没有显示的初始化静态变量会把它设置 ...

  2. Word 打包 zip 并提供下载

    该篇博客记录Java Web项目将word打包zip并提供下载功能的实现和其中遇到的坑,方便后续自己的查看的参照. 1. 后台处理的java 方法 首先将所有的word生成到uploadword目录下 ...

  3. IntelliJ IDEA,代码行宽度超出限制时自动换行

    转自:http://my.oschina.net/angerbaby/blog/471351 当我们使用IDE写代码时,为了保证代码的可阅读性和优雅性,通常会借助IDE的代码风格设置功能,令IDE智能 ...

  4. DTCMS插件的制作实例电子资源管理(一)插件目录结构

    总目录 插件目录结构(一) Admin后台页面编写(二) 前台模板页编写(三) URL重写(四)     本实例旨在以一个实际的项目中的例子来介绍如何在dtcms中制作插件,本系列文章非入门教程,部分 ...

  5. 用node.js实现简单的web服务器

    node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...

  6. STM32 C语言,端口映射

    static XX 有记忆的定义 typedef XX 可以多次定义一个 #ifedf XXX XXX(程序段1) #else XXX(程序段2)

  7. 【JQuery】jQuery.inArray 确定第一个参数在数组中的位置

    函数:jQuery.inArray(value,array,[fromIndex]) 解释:         value:用于在数组中查找是否存在         array:待处理数组.       ...

  8. 获取 AlertDialog自定义的布局 的控件

    AlertDialog自定义的布局 效果图: 创建dialog方法的代码如下: 1 LayoutInflater inflater = getLayoutInflater(); 2 View layo ...

  9. HTML5存储

    Web Storage的概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且每次你请求一个新的页面的时候Cookie都会被发送过去,这样无形中浪费了带宽,另外co ...

  10. jenkins 把包传到远程服务器上

    首先我们在 一台服务器上部署svn,maven,jdk,tomcat,nexus,还有Jenkins. 这里我主要记录Jenkins. 首先我们从网上下载Jenkins的包 wget http://m ...