简体   繁体   中英

How to send nested json object to server using spring mvc + jackson

I have Hotel entity and it has another object called Location .

@Entity
public class Hotel implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      String name;
      @OneToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="LOC_ID")
      Location location;
      //GETTER SETTER
  }

Location object like that:

@Entity
public class Location implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="LOC_ID")
    private Long id;

    String name;

    public Location() {
    }

    public Location(String name) {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
}

I can send Location object to server ( {"name":"MyNewLoc"} ). I can send Hotel object to server with only name it's also ok ( {"name":"NewHotel"} ).

But when I try to send hotel object with name and Location attribute ( {"name":"New Hotel","location":{"name":"MyNewLoc"}} ), I get 400 POST error and this response;

exception:"org.springframework.http.converter.HttpMessageNotReadableException"

message:"Could not read JSON: Template must not be null or empty! (through reference chain: org.maskapsiz.sosyalkovan.domain.Hotel["location"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Template must not be null or empty! (through reference chain: org.maskapsiz.sosyalkovan.domain.Hotel["location"])"

I am using jackson-mapper-asl 1.9.13 and Spring boot.

I added @RestResource(exported = false) and it works for me.

@Entity
@RestResource(exported = false)
public class Location implements Serializable {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM