我尝试加载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]->com.gepick.app.objects.rmo.Pick["match"]->com.gepick.app.objects.rmo.soccer.Match_soccer["ar"]);
nested exception is org.codehaus.jackson.map.JsonMappingException: (was java.lang.NullPointerException)
(through reference chain: java.util.ArrayList[0]->com.gepick.app.objects.rmo.Pick["match"]->com.gepick.app.objects.rmo.soccer.Match_soccer["ar"])
我选择的类别:
@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/