Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从JSON字符串实例化类型的值;没有单字符串构造函数/工厂方法_Java_Javascript_Json_Angularjs_Rest - Fatal编程技术网

Java 无法从JSON字符串实例化类型的值;没有单字符串构造函数/工厂方法

Java 无法从JSON字符串实例化类型的值;没有单字符串构造函数/工厂方法,java,javascript,json,angularjs,rest,Java,Javascript,Json,Angularjs,Rest,我有以下实体: @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"

我有以下实体:

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

    ....

}
和服务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');
    };
}]);
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' }
    });

}]);
插入APLICAO实体时,显示以下错误:

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"])

你能帮我解决这个错误吗?

这个错误告诉你在Grupoaplicaco类中需要一个接受字符串的构造函数方法

@Entity
@Table(name = "GRUPO_APLICACAO")
public class GrupoAplicacao implements Serializable, Entidade {

....

    GrupoAplicacao(String stringJSON){
        setters;
    }

}
谢谢@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类的变量


=)

似乎从javascript发送的是字符串而不是对象。您可以通过发送
JSON.parse(stringGrupoAplicacao)
进行尝试


这样,在服务器端,对象应该自动反序列化,而不需要创建特殊的构造函数

一旦任务和同一问题在以下范围内: +有限时间,+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解决方案非常接近,范围可能稍有不同

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