Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not instantiate value of type from JSON String; no single-String constructor/factory method

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?

like image 629
Gustavo Bitencourt Avatar asked May 30 '14 18:05

Gustavo Bitencourt


2 Answers

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;
    }

}
like image 58
BoatCode Avatar answered Sep 18 '22 15:09

BoatCode


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.

=)

like image 28
Gustavo Bitencourt Avatar answered Sep 17 '22 15:09

Gustavo Bitencourt