Gha*_*ada 6 json resteasy jackson ejb-3.1 jboss7.x
我正在使用RestEasy,Jboss 7和EJB 3.1.我正在创建一个以JSON格式返回数据的RESTful Web服务.
问题是我@ManyToOne在我的一个实体上有一个关系,它会在序列化过程中导致无限递归.我尝试使用杰克逊@JsonIgnore和@JsonBackReference注释来解决这个问题,但似乎它们被完全忽略了,无限递归仍在发生.
这是我的用户类:
class User {
private String userId;
private Role role;
@Id
@Column(name = "\"UserId\"", unique = true, nullable = false, length = 30)
public String getUserId() {
return this.UserId;
}
public void setUserId(String UserId) {
this.UserId = UserId;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "\"RoleId\"", nullable = false)
//I have tried @JsonIgnore without @JsonBackReference
@JsonIgnore
//I have tried @JsonBackReference without @JsonIgnore
//Also I have tried @JsonBackReference and @JsonIgnore together
@JsonBackReference("role-User")
public Role getRole() {
return this.role;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的角色类的一部分:
@JsonManagedReference("role-User")
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "role")
public Set<User> getUsers() {
return this.users;
}
Run Code Online (Sandbox Code Playgroud)
我已经读过某个地方,我应该用我的应用程序注册Jackson,以便能够使用常规的Jaxb注释,所以我创建了一个类
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
public JacksonContextResolver() {
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getDeserializationConfig().setAnnotationIntrospector(
introspector);
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
}
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
Run Code Online (Sandbox Code Playgroud)
上面提到的问题已JaxbAnnotationIntrospector()被弃用.
请:
JaxbAnnotationIntrospector()?感谢,对这些问题的答案表示赞赏.
更新:
现在我已经排除了resteasy-jackson-provider使用,jboss-deployment-structure.xml而我正在使用Jettison.我还想知道我怎么能用杰克逊!
小智 9
这里的问题似乎与使用Set<User>而不是List<User>. 我遇到了完全相同的问题并从更改Set<User>为List<User>修复了这个问题,否则我总是Infinite recursion从杰克逊那里得到错误。我不知道这是否真的是 Jackson 中的错误,还是在使用Set.
用import com.fasterxml.jackson.annotation.JsonBackReference;而不是import org.codehaus.jackson.annotate.JsonBackReference;.
这对我来说是个问题.
小智 2
看,我有一些和你类似的东西,并且对我来说工作得很好......
@JsonBackReference("promotion-travel")
@ManyToOne(cascade = CascadeType.ALL)
@JoinTable(name="PROMOTION_TRAVEL_REL",
joinColumns = @JoinColumn(name="TRAVEL_ID"),
inverseJoinColumns = @JoinColumn(name="PROMOTION_ID")
)
public Promotion getPromotion(){...
Run Code Online (Sandbox Code Playgroud)
这就是我在 Entity1 上拥有的
@JsonManagedReference("promotion-travel")
@OneToMany(mappedBy="promotion")
public List<Travel> getTravelList(){...
Run Code Online (Sandbox Code Playgroud)
我不知道将注释移到顶部是否会改变任何内容,但这是我唯一能看到的......
干杯,
| 归档时间: |
|
| 查看次数: |
13701 次 |
| 最近记录: |