How to use @RestController (Spring) with a child List of object
I'm trying to create a REST service with Spring.
Everything works until I try to add a List of object (CartItem) to my main object (Cart).
This is my main object
@Entity
@Table(name="cart")
public class Cart implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
/*when I add this I get the error. If I remove this, the
REST service works*/
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
//getter, setter, constructors, other fields ecc.
}
This is the object inside the List:
@Entity
@Table(name="cart_item")
public class CartItem implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToOne(targetEntity = Product.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName="productId", name="product_id" )
private Product product;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
//getter, setter, constructors, other fields ecc.
}
This is my controller
@RestController
@RequestMapping(value="rest/cart")
public class CartRestController {
...
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Cart> readAll() {
return cartService.read();
}
...
}
I get this error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/webstore] threw exception [Request processing failed; nested exception
is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:...
I suppose that I had to manage the List inside the Cart object in a particular manner, maybe because i'm using JPA, but I still didn't find a solution on the internet.
Can anyone help me?
spring rest spring-restcontroller
add a comment |
I'm trying to create a REST service with Spring.
Everything works until I try to add a List of object (CartItem) to my main object (Cart).
This is my main object
@Entity
@Table(name="cart")
public class Cart implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
/*when I add this I get the error. If I remove this, the
REST service works*/
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
//getter, setter, constructors, other fields ecc.
}
This is the object inside the List:
@Entity
@Table(name="cart_item")
public class CartItem implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToOne(targetEntity = Product.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName="productId", name="product_id" )
private Product product;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
//getter, setter, constructors, other fields ecc.
}
This is my controller
@RestController
@RequestMapping(value="rest/cart")
public class CartRestController {
...
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Cart> readAll() {
return cartService.read();
}
...
}
I get this error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/webstore] threw exception [Request processing failed; nested exception
is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:...
I suppose that I had to manage the List inside the Cart object in a particular manner, maybe because i'm using JPA, but I still didn't find a solution on the internet.
Can anyone help me?
spring rest spring-restcontroller
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52
add a comment |
I'm trying to create a REST service with Spring.
Everything works until I try to add a List of object (CartItem) to my main object (Cart).
This is my main object
@Entity
@Table(name="cart")
public class Cart implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
/*when I add this I get the error. If I remove this, the
REST service works*/
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
//getter, setter, constructors, other fields ecc.
}
This is the object inside the List:
@Entity
@Table(name="cart_item")
public class CartItem implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToOne(targetEntity = Product.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName="productId", name="product_id" )
private Product product;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
//getter, setter, constructors, other fields ecc.
}
This is my controller
@RestController
@RequestMapping(value="rest/cart")
public class CartRestController {
...
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Cart> readAll() {
return cartService.read();
}
...
}
I get this error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/webstore] threw exception [Request processing failed; nested exception
is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:...
I suppose that I had to manage the List inside the Cart object in a particular manner, maybe because i'm using JPA, but I still didn't find a solution on the internet.
Can anyone help me?
spring rest spring-restcontroller
I'm trying to create a REST service with Spring.
Everything works until I try to add a List of object (CartItem) to my main object (Cart).
This is my main object
@Entity
@Table(name="cart")
public class Cart implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
/*when I add this I get the error. If I remove this, the
REST service works*/
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
//getter, setter, constructors, other fields ecc.
}
This is the object inside the List:
@Entity
@Table(name="cart_item")
public class CartItem implements Serializable{
...
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToOne(targetEntity = Product.class, cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName="productId", name="product_id" )
private Product product;
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
//getter, setter, constructors, other fields ecc.
}
This is my controller
@RestController
@RequestMapping(value="rest/cart")
public class CartRestController {
...
@RequestMapping(value = "/", method = RequestMethod.GET)
public List<Cart> readAll() {
return cartService.read();
}
...
}
I get this error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path
[/webstore] threw exception [Request processing failed; nested exception
is org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON: Infinite recursion (StackOverflowError); nested
exception is com.fasterxml.jackson.databind.JsonMappingException:
Infinite recursion (StackOverflowError) (through reference chain:...
I suppose that I had to manage the List inside the Cart object in a particular manner, maybe because i'm using JPA, but I still didn't find a solution on the internet.
Can anyone help me?
spring rest spring-restcontroller
spring rest spring-restcontroller
edited Nov 22 '18 at 9:16
MDP
asked Nov 21 '18 at 14:47
MDPMDP
1,74683971
1,74683971
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52
add a comment |
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52
add a comment |
2 Answers
2
active
oldest
votes
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
- a Cart gets serialized to JSON
- all the CartItems inside it get serialized to JSON
- the Cart property inside CartItem get serialized to JSON
- the CartItems inside the cart get serialized to json, etc. etc.
- the Cart property inside CartItem get serialized to JSON
- all the CartItems inside it get serialized to JSON
You will probably want to exclude the CartItem.cart field from serialization by marking it with the @JsonIgnore
annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
add a comment |
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a @JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with @oneToMany,@ManyToOne or @ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding @JsonIgnore,overriding toString() and equal() method.
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414596%2fhow-to-use-restcontroller-spring-with-a-child-list-of-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
- a Cart gets serialized to JSON
- all the CartItems inside it get serialized to JSON
- the Cart property inside CartItem get serialized to JSON
- the CartItems inside the cart get serialized to json, etc. etc.
- the Cart property inside CartItem get serialized to JSON
- all the CartItems inside it get serialized to JSON
You will probably want to exclude the CartItem.cart field from serialization by marking it with the @JsonIgnore
annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
add a comment |
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
- a Cart gets serialized to JSON
- all the CartItems inside it get serialized to JSON
- the Cart property inside CartItem get serialized to JSON
- the CartItems inside the cart get serialized to json, etc. etc.
- the Cart property inside CartItem get serialized to JSON
- all the CartItems inside it get serialized to JSON
You will probably want to exclude the CartItem.cart field from serialization by marking it with the @JsonIgnore
annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
add a comment |
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
- a Cart gets serialized to JSON
- all the CartItems inside it get serialized to JSON
- the Cart property inside CartItem get serialized to JSON
- the CartItems inside the cart get serialized to json, etc. etc.
- the Cart property inside CartItem get serialized to JSON
- all the CartItems inside it get serialized to JSON
You will probably want to exclude the CartItem.cart field from serialization by marking it with the @JsonIgnore
annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
This is a serialization recursion problem, it happens because CartItem has a bidirectional mapping back to Cart. So what happens is that
- a Cart gets serialized to JSON
- all the CartItems inside it get serialized to JSON
- the Cart property inside CartItem get serialized to JSON
- the CartItems inside the cart get serialized to json, etc. etc.
- the Cart property inside CartItem get serialized to JSON
- all the CartItems inside it get serialized to JSON
You will probably want to exclude the CartItem.cart field from serialization by marking it with the @JsonIgnore
annotation.
It is only too easy to expose far too much information to the outside world if you use JPA entities directly inside your webservices. Jackson actually has a useful feature called a JsonView which allows you to define which properties get exposed, you can even tailor it per webservice call if you want.
answered Nov 22 '18 at 10:02
GimbyGimby
3,87122536
3,87122536
add a comment |
add a comment |
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a @JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with @oneToMany,@ManyToOne or @ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding @JsonIgnore,overriding toString() and equal() method.
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
add a comment |
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a @JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with @oneToMany,@ManyToOne or @ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding @JsonIgnore,overriding toString() and equal() method.
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
add a comment |
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a @JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with @oneToMany,@ManyToOne or @ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding @JsonIgnore,overriding toString() and equal() method.
Never ending list? Did you mean a stackOverFlow exception?
If the situation is just like I said,then you should check something like fetch type and the entities' toString() or equal() method or something like that.
For example,there are to entities named A and B and their relationship is one to many(A is the one).If you config both of their fetchType as Eager,then when jpa query A,it will query B too.But B also contains A,so jpa will query A again.This kind of circle loop will cause a stackOverFlow.
By the way, how about providing more info about your problem like the Exception name?It's too hard for me to give you a specific solution,All I can do is to tell you some experiences I have met before.
Well,I created a small project with SpringBoot 2.1.0 and MySql.
It's my cartItem
public class CartItem {
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@JsonIgnore
@ManyToOne
@JoinColumn(name="cart_id", nullable=false)
private Cart cart;
}
and my cart:
public class Cart {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
@Column(name="id")
private Integer id;
@OneToMany(mappedBy="cart", fetch = FetchType.EAGER)
private List<CartItem> cartItems;
}
Controller is as same as you wrote.After adding a @JsonIgnore to cart filed of CartItem,circle loop is over(before i do that,the program did had a circle loop problem).
Every time you use jpa with @oneToMany,@ManyToOne or @ManyToMany,you should be careful about this problem.This circular reference case could happen when instantiating a object, printing a object or something like this.And of course there is a lot of way to solve it like changing fetch type to LAZY,adding @JsonIgnore,overriding toString() and equal() method.
edited Nov 22 '18 at 10:32
answered Nov 22 '18 at 2:50
AokoQinAokoQin
794
794
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
add a comment |
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
Hi @AokoQin, I edited my post, now the problem should be clearer. The problem is solved by adding JsonIgnore in the class CartItem, on the field that mapped back the Cart class, as suggested by the user Gimby. As you said, it was a cirlce loop that causes the error.
– MDP
Nov 22 '18 at 9:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414596%2fhow-to-use-restcontroller-spring-with-a-child-list-of-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please add the error message. This is just a part of the Stacktrace.
– markusw
Nov 21 '18 at 15:06
It maybe a lazy load problem. please make sure all lazy load is fetched in the same transaction if not use other annotation json to ignore it.
– huy
Nov 21 '18 at 15:50
@Gimby adding JsonIgnore the problem is solved. The problem was in the class CartItem, on the field that mapped back the Cart class
– MDP
Nov 22 '18 at 9:12
I edited my code, now the error is clearer. Thanks everybody for the help.
– MDP
Nov 22 '18 at 9:16
yesy, I did it :)
– MDP
Nov 22 '18 at 10:52