相关疑难解决方法(0)

杰克逊 - 具有双向关系的实体的序列化(避免周期)

我有两个实体:

Parent {
   Child[] children;
}

and 

Child {
   Parent parent;
}
Run Code Online (Sandbox Code Playgroud)

我知道@JsonBackReference@JsonManagedReference.如果我正在序列化实例,那么它们很好Parent.

但我还需要传输实例,Child并希望parent填充该字段.

换一种说法:

  1. Parent它的序列化应该有,children但他们的父字段可能是空的(可以通过使用json引用注释来解决).
  2. Child它的序列化应该parent与他们children(但children不必parent填充.

有没有办法使用标准的Jackson功能来解决它​​?

即跳过已经序列化的实体的序列化,而不是标记符合条件或不符合序列化条件的字段.

java json cycle jackson bidirectional-relation

43
推荐指数
2
解决办法
3万
查看次数

Spring REST,JSON"无法处理托管/后向引用'defaultReference'"415不支持的媒体类型

我正在尝试使用Spring boot/Spring RestController后端从AngularJS前端POST到http:// localhost:9095/translators.

我可以做一个GET,响应如下:

[{"userId":1,"firstName":"John","lastName":"Doe","emailId":"john.doe@inc.com","languages":[{"languageId":1,"languageCode":"gb","source":true}],"translations":[{"translationId":3,"sourceId":1,"sourceText":"Hello","targetId":null,"targetText":null,"translationStatus":"DUE"}],"userType":"TRANSLATOR"}
Run Code Online (Sandbox Code Playgroud)

当我发布下面的json时,我得到错误响应

发布数据:

{
                    firstName: "zen",
                    lastName: "cv",
                    emailId: "email",
                    userType: "TRANSLATOR",
                    languages : [{languageId:1,languageCode:"gb",source:true}]
}
Run Code Online (Sandbox Code Playgroud)

错误:

{
timestamp: 1422389312497
status: 415
error: "Unsupported Media Type"
exception: "org.springframework.web.HttpMediaTypeNotSupportedException"
message: "Content type 'application/json' not supported"
path: "/translators"
}
Run Code Online (Sandbox Code Playgroud)

我确保我的控制器具有正确的Mediatype注释.

@RestController
@RequestMapping("/translators")
public class TranslatorController {
    @Autowired
    private UserRepository repository;

    @RequestMapping(method = RequestMethod.GET)
    public List findUsers() {
        return repository.findAll();
    }

    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    public User findUser(@PathVariable Long userId) {
        return …
Run Code Online (Sandbox Code Playgroud)

java json hibernate jackson spring-boot

14
推荐指数
3
解决办法
2万
查看次数