java - AngularJS 应用程序无法从服务器获取列表(Java Spring)

标签 java angularjs spring hibernate jackson

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

Spring Controller :

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

我将 Controller 更改为:

@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 和 messagesgae:

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

最佳答案

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

您可以看到您的 Match_soccer映射之前初始化,或者您可以注释您的 Pick 类有了这个@JsonInclude(Ininclude.NON_NULL)

This将指示映射器忽略映射中的所有null字段

关于java - AngularJS 应用程序无法从服务器获取列表(Java Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154526/

相关文章:

java - Spring Bean Map 重复键

java - orderby 中的绑定(bind)值不适用于 NamedParameterJdbcTemplate

java - seam-2.2.2 java 8 流口水更新

java - 将字符串 append 到 native Java 数组

java - charAt() 或子字符串?哪个更快?

javascript - AngularJS 下拉菜单不显示选定的值

java - 过滤元素的设计模式

javascript - 创建在应用程序加载时调用方法的 AngularJS 对象

angularjs - ng-show 中 $index 的可访问性

java - spring 4.1.6 发布构建问题