无法从 JSON 字符串实例化类型值;没有单字符串构造函数/工厂方法
Posted
技术标签:
【中文标题】无法从 JSON 字符串实例化类型值;没有单字符串构造函数/工厂方法【英文标题】:Can not instantiate value of type from JSON String; no single-String constructor/factory method 【发布时间】:2014-07-20 14:11:48 【问题描述】:我有以下实体:
@Entity
@Table(name="APLICACAO")
public class Aplicacao implements Serializable, Entidade
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="CD_APLICACAO")
private Long codigo;
@Column(name="NM_APLICACAO")
@NotNull
private String nome;
@ManyToOne
@JoinColumn(name="CD_GRUPO")
private GrupoAplicacao grupoAplicacao;
....
还有:
@Entity
@Table(name = "GRUPO_APLICACAO")
public class GrupoAplicacao implements Serializable, Entidade
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CD_GRUPO")
private Long codigo;
@Column(name = "NM_GRUPO")
@NotNull
private String nome;
@OneToMany(mappedBy = "grupoAplicacao",fetch=FetchType.EAGER)
private List<Aplicacao> listaAplicacao;
@OneToMany(mappedBy = "grupoAplicacao",fetch=FetchType.EAGER)
private List<GrupoAplicacaoUsuario> listaGrupoAplicacaoUsuario;
....
我的 html 调用控制器“aplicacoesCreateController”,如下代码:
<div class="col-xs-12" ng-controller="aplicacoesCreateController">
<form class="form-horizontal form-medium center" role="form" name="form" ng-submit="save()">
<div class="form-group">
<label for="nome" class="col-xs-4 control-label">Nome</label>
<div class="col-xs-8">
<input type="text" class="form-control input-sm" id="nome" placeholder="Nome" required ng-model="aplicacao.nome">
</div>
</div>
<div class="form-group">
<label for="nome" class="col-xs-4 control-label">Nome</label>
<div class="col-xs-8">
<select class="form-control" required ng-model="aplicacao.grupoAplicacao">
<option ng-repeat="grupo in grupos" value=" grupo ">
grupo.nome
</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-4 col-xs-8">
<button type="submit" class="btn btn-lg btn-size-md">Salvar</button>
</div>
</div>
</form>
</div>
还有我的控制器 javascript:
app.controller('aplicacoesCreateController', ['$scope','aplicacoesService','$location','reloadService','gruposService',
function ($scope,aplicacoesService,$location,reloadService,gruposService)
$scope.grupos = gruposService.list();
$scope.save = function ()
aplicacoesService.create($scope.aplicacao);
reloadService.on('#/aplicacoes');
;
]);
和服务 JavaScript:
app.factory('gruposService', ['$resource', function ($resource)
return $resource('resources/grupo-aplicacao', ,
'list': method: 'GET', isArray: true
);
]);
其他服务:
app.factory('aplicacoesService', ['$resource', function ($resource)
return $resource('resources/aplicacao', ,
'list': method: 'GET', isArray: true ,
'create': method: 'POST'
);
]);
插入 aplicacao 实体时,显示以下错误:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class br.com.techpeople.grape.entity.GrupoAplicacao] from JSON String; no single-String constructor/factory method (through reference chain: br.com.techpeople.grape.entity.Aplicacao["grupoAplicacao"])
你能帮我解决这个错误吗?
【问题讨论】:
【参考方案1】:错误告诉您,您的 GrupoAplicacao 类中需要一个接受字符串的构造方法。
@Entity
@Table(name = "GRUPO_APLICACAO")
public class GrupoAplicacao implements Serializable, Entidade
....
GrupoAplicacao(String stringJSON)
setters;
【讨论】:
【参考方案2】:谢谢@BoatCode!
我在构造函数中做了以下操作:
public GrupoAplicacao(String grupoAplicacaoJSON)
Gson gson = new Gson();
GrupoAplicacao grupoAplicacao = gson.fromJson(grupoAplicacaoJSON, GrupoAplicacao.class);
this.codigo = grupoAplicacao.getCodigo();
this.nome = grupoAplicacao.getNome();
this.listaAplicacao = grupoAplicacao.getListaAplicacao();
this.listaGrupoAplicacaoUsuario = grupoAplicacao.getListaGrupoAplicacaoUsuario();
添加lib Gson并设置GrupoAplicacao类的变量。
=)
【讨论】:
【参考方案3】:一旦任务和相同问题在以下范围内: +限时, +ee: +jax-rs && +persistence, +gson;我已经解决了它:
@Entity
@XmlRootElement
@Table(name="element")
public class Element implements Serializable
public Element(String stringJSON)
Gson g = new Gson();
Element a = g.fromJson(stringJSON, this.getClass());
this.setId(a.getId());
this.setProperty(a.getProperty());
public Element()
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
...
它非常接近 Gustavo 的 Bitencourt 解决方案,但范围可能略有不同。
【讨论】:
【参考方案4】:看来,从 javascript 中,您发送的是字符串而不是对象。您可以发送JSON.parse(stringGrupoAplicacao)
尝试。
这样,在服务器端,对象应该被自动反序列化,而不需要创建一个特殊的构造函数。
【讨论】:
以上是关于无法从 JSON 字符串实例化类型值;没有单字符串构造函数/工厂方法的主要内容,如果未能解决你的问题,请参考以下文章
org.codehaus.jackson.map.JsonMappingException:无法从 JSON 字符串实例化类型 [简单类型,类 models.Job] 的值
Jackson ObjectMapper 无法反序列化 POJO,抛出异常:没有找到适合类型 [...] 的构造函数:无法从 JSON 对象实例化