Announcement Announcement Module
Collapse
No announcement yet.
Unable to convert @NodeEntity object to JSON with Jackson Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to convert @NodeEntity object to JSON with Jackson

    I've got an application, which retrieves entities from Neo4j datastore and sends them as JSON. However, instead of seeing nice JSON output I was greeted with an exception (similar to listed below). I've simplified the code to this:

    Handler mapping

    Code:
            @RequestMapping("/test")
    	public @ResponseBody Video test () {
    		Video v = new Video();
    		v.setTitle("title");
    		v.setId(1L);
    		return v;
    	}
    Video class

    Code:
    @NodeEntity
    public class Video {
    	@GraphId 
    	private Long id;
    	private String title;
    	
    	public Long getId() {
    		return id;
    	}
    	public void setId(Long id) {
    		this.id = id;
    	}
    	
    	public String getTitle() {
    		return title;
    	}
    	public void setTitle(String title) {
    		this.title = title;
    	}
    	
    }
    If I disable AspectJ weaver everything works fine, but otherwise I get this

    No serializer found for class org.springframework.core.convert.support.GenericCo nversionService and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: spektrum.limelight.domain.Video["entityState"]->org.springframework.data.neo4j.fieldaccess.Detach edEntityState["template"]->org.springframework.data.neo4j.support.Neo4jTempl ate["infrastructure"]->org.springframework.data.neo4j.support.MappingInf rastructure["conversionService"])
    I would like to know if it's my fault or the bytecode enhancer is to blame

  • #2
    Imho you can exclude properties from Serialization with Jackson,

    could you try this for the introduced field entityState

    Perhaps use some information on this page: http://www.cowtowncoder.com/blog/arc...entry_443.html

    Probably a JsonView is the most sensible one, as the other Jackson options require SDN to pull in Jackson as a hard dependency . Or an DTO for the JSON. (probably also a superclass, where only the subclass contains @NodeEntity).

    I can also try to make the getEntityState() package protected but have to see if that works out.

    Michael

    Comment


    • #3
      Thank you very much, Michael. I didn't realize that serialization failed because of the introduced field, got to pay more attention next timeю I've changed my code to this and now everything works like it should.

      Code:
      @NodeEntity
      @JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE)
      public class Video {
      	@GraphId 
      	@JsonProperty(value = "id")
      	private Long id;
      	@JsonProperty(value = "title")
      	private String title;
      	
      	public Video () {
      		
      	}
      	
      	public Long getId() {
      		return id;
      	}
      	public void setId(Long id) {
      		this.id = id;
      	}
      	
      	public String getTitle() {
      		return title;
      	}
      	public void setTitle(String title) {
      		this.title = title;
      	}
      	
      }
      Thanks again, Michael, you've prevented many plucked hairs!

      Comment


      • #4
        Yep, a good thing to know. Especially because I was going to get to that point and probably would have had the same problems, and now I know how to avoid it.

        Thanks guys for posting

        Mark

        Comment


        • #5
          Despite tunning your Serializer in order to correctly Serialize NodeEntity, wouldn't be better if spring-data-neo4j provide a non-intrusive solution for this? In my opinion, any field introduced by the spring-data-neo4j Aspects on an Entity should be marked as transient. This way, almost all serializers will ignore them by default. I don't see any explicit reason anyone would want to serialize those fields (mainly Neo4jTemplate, which is a stateful object, thus should never be serialized).

          Comment


          • #6
            This is on our roadmap and will be handled correctly in the future.

            The problem is bigger with AJ, you have to:

            * add another field to hold the node or relationship-id
            * disable aspectj-field interception for this instance
            * copy all the state from all fields and relationships to the entity

            So it is not just making things transient. It is not the transient field jackson looks at but rather the getter for EntityState that exists for interoperability in the library.

            Michael

            Comment


            • #7
              Dear Michael,

              Thank you for your response. I got the problem, indeed there are a lot of things to consider. I'm new to spring-data-neo4j yet, but I'm progressing and I hope I can give more detailed feedback and/or solutions to issues in a near future.

              Best regards,

              Daniel

              Comment


              • #8
                Thanks for your feedback so far, looking forward to your forward progress.

                Comment


                • #9
                  Old thread but still relevant. In my opinion the easiest solution is

                  Code:
                  @JsonIgnoreProperties({"entityState", "nodeId", "persistentState", "relationshipTo", "template"})

                  Comment

                  Working...
                  X