【问题标题】:AngularJS application can`t get List from Server (Java Spring)AngularJS 应用程序无法从服务器获取列表(Java Spring)
【发布时间】:2015-08-22 09:32:04
【问题描述】:

我尝试将List<Pick> 加载到我的前端应用程序表单服务器。在服务器上,我有 Spring 应用程序,我使用 hibernate 从数据库中加载选取对象

弹簧控制器:

@RequestMapping(value = "/web2/getActivePicks", method =  RequestMethod.POST)
public @ResponseBody List<Pick>  getActivePicks() {
    PickDAO pd = new PickDAO();
    List<Pick> picks = pd.getPicksFrom(new Date()); 
    return picks;   
}

当我尝试获取此选择列表时,我得到了 `

HTTP Status 500 - Could not write JSON: 
(was java.lang.NullPointerException) 
(through reference chain: java.util.ArrayList[0]-&gt;com.gepick.app.objects.rmo.Pick[&quot;match&quot;]-&gt;com.gepick.app.objects.rmo.soccer.Match_soccer[&quot;ar&quot;]); 
nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException) 
(through reference chain: java.util.ArrayList[0]-&gt;com.gepick.app.objects.rmo.Pick[&quot;match&quot;]-&gt;com.gepick.app.objects.rmo.soccer.Match_soccer[&quot;ar&quot;])

我的选择课:

@Entity
public class Pick {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int pid;

@OneToOne
private Match_soccer match;

@OneToOne
private Algo1 algo;

@Column(length = 100)
private String pick;

@Column(length = 5)
private double plimit;
....

我将控制器更改为:

@RequestMapping(value = "/web2/getActivePicks", method =     RequestMethod.POST)
public @ResponseBody JSONArray  getActivePicks() {
    PickDAO pd = new PickDAO();
    JSONArray picks = new JSONArray(pd.getPicksFrom(new Date()));
    return picks;   
}

但现在我得到 http 状态 406 和 messgae:

The resource identified by this request is only capable of generating responses 
with characteristics not acceptable according to the request "accept" headers

【问题讨论】:

    标签: java angularjs spring hibernate jackson


    【解决方案1】:

    似乎当Jackson,您的JSON mapper 正在尝试转换Pick 实例时,Match_soccer 为空并引发异常。

    您可以看到您的Match_soccer映射 之前被初始化,或者您可以使用@JsonInclude(Include.NON_NULL)注释您的Pick 类。

    This 将指示 mapper 忽略 mapping

    中的所有 null 字段

    【讨论】:

      【解决方案2】:

      看起来 Spring 没有将 List 转换为 JSON 的默认方法,因此无法通过 REST 传递它。你能先把它转换成JSONArray吗?

      【讨论】:

      • 我换了控制器,查一下:)
      【解决方案3】:

      问题在于匹配对象,因为 hr 和 ar 属性为空。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 2016-01-09
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        相关资源
        最近更新 更多