2017-03-02 67 views
2

我有兩個實體我序列化併發送到客戶端使用春天框架4.2.5 + jacksonjson 2.7.5。我單位有以下幾種:序列化已經有ID爲POJO(java.lang.String)

@Entity 
@Table(name = "entrada") 
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="idEntrada") 
public class EntradaTest implements Serializable { 
private static final long serialVersionUID = 1L; 

@Id 
@Column(name = "id_entrada", unique = true, nullable = false) 
private String idEntrada; 

@Column(nullable = false, length = 5) 
private String codigo; 

private Integer recibidos; 

@ManyToOne 
@JsonIdentityReference(alwaysAsId = true) 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idEmpleado", scope = Empleado.class) 
@JoinColumn(name = "id_pesador_bruto") 
private Empleado pesadorBruto; 

@ManyToOne 
@JsonIdentityReference(alwaysAsId = true) 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "dEmpleado", scope = Empleado.class) 
@JoinColumn(name = "id_pesador_tara") 
private Empleado pesadorTara; 

@ManyToOne 
@JsonIdentityReference(alwaysAsId = true) 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idEmpleado", scope = Empleado.class) 
@JoinColumn(name = "id_representante_bruto") 
private Empleado representanteBruto; 

@ManyToOne 
@JsonIdentityReference(alwaysAsId = true) 
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "idEmpleado", scope = Empleado.class) 
@JoinColumn(name = "id_representante_tara") 
private Empleado representanteTara; 

@ManyToOne 
@JoinColumn(name = "id_oficina", nullable = false) 
private Entidad oficina; 
} 

@Entity 
@Table(name = "empleado") 
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="idEmpleado", scope = Empleado.class) 
class Empleadoest implements Serializable { 
private static final long serialVersionUID = 1L; 

@Id 
@Column(name = "id_empleado", unique = true, nullable = false) 
private String idEmpleado; 

@Column(nullable = false, length = 125) 
private String nombre; 

@OneToMany(mappedBy = "pesadorBruto") 
private Set<Entrada> entradasPesadorBruto; 

@OneToMany(mappedBy = "pesadorTara") 
private Set<Entrada> entradasPesadorTara; 

@OneToMany(mappedBy = "representanteBruto") 
private Set<Entrada> entradasRepresentanteBruto; 

@OneToMany(mappedBy = "representanteTara") 
private Set<Entrada> entradasRepresentanteTara; 
} 

我的REST服務端點是

@RequestMapping(value = "/entradas/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
public ResponseEntity<Entrada> getEntrada(@PathVariable("id") String idEntrada) { 
Entrada entrada = entradaService.get(idEntrada); 
if (entrada == null) { 
return new ResponseEntity<Entrada>(HttpStatus.NOT_FOUND); 
} 
return new ResponseEntity<Entrada>(entrada, HttpStatus.OK); 
} 

@RequestMapping(value = "/entradas/", method = RequestMethod.POST) 
public ResponseEntity<Void> createEntrada(@RequestBody Entrada entrada, UriComponentsBuilder ucBuilder) { 

entradaService.save(entrada); 

HttpHeaders headers = new HttpHeaders(); 
headers.setLocation(ucBuilder.path("/entradas/{id}").buildAndExpand(entrada.getIdEntrada()).toUri()); 
return new ResponseEntity<Void>(headers, HttpStatus.CREATED); 
} 

我向客戶端發送的序列化的數據是這樣的:

{ 
"idEntrada": "e375ecf9-dabd-4c76-8813-0679818f9590", 
"codigo": "378-2", 
"recibidos": 0, 
"pesadorBruto": "0c23c490-a54a-495d-9447-dc6227520646", 
"pesadorTara": "874dfe26-11cb-48e4-916e-bf8a83187dcb", 
"representanteBruto": "5fb567af-805a-40dc-84bc-8f6038d8cd2f", 
"representanteTara": "5fb567af-805a-40dc-84bc-8f6038d8cd2f", 
"oficina": { 
"idEntidad": "f3964add-3ae8-4392-bafc-cffb9643ec15", 
"nombre": "O.P.C.", 
}, 
} 

但是,當我嘗試發送數據返回到服務器如下創建一個新的實體

{ 
"idEntrada":"e375ecf9-dabd-4c76-8813-0679818f9590", 
"codigo":"378-2", 
"pesadorBruto":{ 
"idEmpleado":"0c23c490-a54a-495d-9447-dc6227520646", 
"nombre":"J. CABRERA", 
}, 
"pesadorTara":{ 
"idEmpleado":"874dfe26-11cb-48e4-916e-bf8a83187dcb", 
"nombre":"L. A. DELGADO", 
}, 
"representanteBruto":{ 
"idEmpleado":"5fb567af-805a-40dc-84bc-8f6038d8cd2f", 
"nombre":"C. MARQUEZ", 
}, 
"representanteTara":{ 
"idEmpleado":"5fb567af-805a-40dc-84bc-8f6038d8cd2f", 
"nombre":"C. MARQUEZ", 
}, 
"oficina":{ 
"idEntidad":"f3964add-3ae8-4392-bafc-cffb9643ec15", 
"nombre":"O.P.C.", 
}, 
} 

傑克遜失敗:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: 
Could not read document: Already had POJO for id (java.lang.String) 
[[ObjectId: key=5fb567af-805a-40dc-84bc-8f6038d8cd2f, 
type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, 
scope=lt.ciader.model.Empleado]] 
(through reference chain: lt.ciader.model.Entrada["representanteTara"]->lt.ciader.model.Empleado["idEmpleado"]); 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: 
Already had POJO for id (java.lang.String) 
[[ObjectId: key=5fb567af-805a-40dc-84bc-8f6038d8cd2f, 
type=com.fasterxml.jackson.databind.deser.impl.PropertyBasedObjectIdGenerator, 
scope=lt.ciader.model.Empleado]] 
(through reference chain: lt.ciader.model.Entrada["representanteTara"]->lt.ciader.model.Empleado["idEmpleado"]) 

我知道representanteTara是相同的SA representateBruto但它可以是相同的就業acoording到我的模型,甚至pesadorTara和PesadorBruto了。

當我發送數據與differents empleados我的服務獲取數據並序列化它沒有問題。 我搜索了一個解決方案,但我找不到幫助我的答案。我回顧:

JsonMappingException: Already had POJO for id

Jackson Already had POJO for id

Spring Rest error the second time that saves Object with child properties that references the same father the second time: Already had POJO for id

https://groups.google.com/forum/#!topic/jackson-user/TSloBi1C_qk

https://github.com/edwinquaihoi/jsonidentitymanytomany/issues/1

https://github.com/FasterXML/jackson-databind/issues/266

我做錯了什麼。我該如何解決這個問題。

回答

1

經過多次測試,我找到了解決方案。問題在於傑克遜因爲同一個實體存在兩次而失敗。解決的辦法是在塞康實例只發送平原ID,未包裹在對象:

{ 
"idEntrada":"e375ecf9-dabd-4c76-8813-0679818f9590", 
"codigo":"378-2", 
"pesadorBruto":{ 
"idEmpleado":"0c23c490-a54a-495d-9447-dc6227520646", 
"nombre":"J. CABRERA", 
}, 
"pesadorTara":{ 
"idEmpleado":"874dfe26-11cb-48e4-916e-bf8a83187dcb", 
"nombre":"L. A. DELGADO", 
}, 
"representanteBruto":{ 
"idEmpleado":"5fb567af-805a-40dc-84bc-8f6038d8cd2f", 
"nombre":"C. MARQUEZ", 
}, 

"representanteTara": "5fb567af-805a-40dc-84bc-8f6038d8cd2f",

"oficina":{ 
"idEntidad":"f3964add-3ae8-4392-bafc-cffb9643ec15", 
"nombre":"O.P.C.", 
}, 
} 
相關問題