Can not instantiate value of type from JSON String; no single-String constructor/factory method
52,819
Solution 1
The error is telling you that you need a constructor method in your GrupoAplicacao class which accepts a string.
@Entity
@Table(name = "GRUPO_APLICACAO")
public class GrupoAplicacao implements Serializable, Entidade {
....
GrupoAplicacao(String stringJSON){
setters;
}
}
Solution 2
Once the task and the same issue was in scope of: +limited time, +ee: +jax-rs && +persistence, +gson; I have solved it then as:
@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;
...
}
It's pretty close to Gustavo's Bitencourt solution, with maybe a bit different scope.
Solution 3
Thank you @BoatCode!
I did the following in my constructor:
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();
}
Add the lib Gson and set the variables of GrupoAplicacao class.
=)
Author by
Admin
Updated on July 16, 2022Comments
-
Admin almost 2 years
I have following entities:
@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; .... }
And also:
@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; .... }
My HTML call controller "aplicacoesCreateController", like code bellow:
<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>
And my Controller 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'); }; }]);
And Services JavaScript:
app.factory('gruposService', ['$resource', function ($resource) { return $resource('resources/grupo-aplicacao', {}, { 'list': { method: 'GET', isArray: true } }); }]);
And other service:
app.factory('aplicacoesService', ['$resource', function ($resource) { return $resource('resources/aplicacao', {}, { 'list': { method: 'GET', isArray: true }, 'create': { method: 'POST' } }); }]);
When insert aplicacao entity, is show me the following error:
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"])
Could you help me with this error?