我有两个实体:
Parent {
Child[] children;
}
and
Child {
Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
我知道@JsonBackReference和@JsonManagedReference.如果我正在序列化实例,那么它们很好Parent.
但我还需要传输实例,Child并希望parent填充该字段.
换一种说法:
Parent它的序列化应该有,children但他们的父字段可能是空的(可以通过使用json引用注释来解决).Child它的序列化应该parent与他们children(但children不必parent填充.有没有办法使用标准的Jackson功能来解决它?
即跳过已经序列化的实体的序列化,而不是标记符合条件或不符合序列化条件的字段.
我正在尝试使用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)