Recursive JSON input causes SyntaxError: Unexpected end of JSON input at JSON.parse
Recursive JSON input causes SyntaxError: Unexpected end of JSON input at JSON.parse
我已经处理了几个小时,我似乎可以解决这个问题。我有两个实体 Products 和 Customer,其中一个客户可以拥有一个产品,而一个产品可能有很多客户。在我的 SQL 服务器管理工作室中,产品 table 的主键作为外键存在于客户 table.
中
我在下面的代码中展示了两个实体。问题是客户 "c" 被递归附加到 "myproducts",这是 JSON 中的 mappedBy 属性,当我在浏览器 window 上查看控制台时显示。 (请参阅下面错误中的嵌套对象 "myproducts" 和 "c")
我正在使用 GET 方法 API 在屏幕上显示客户。
Products.java
@Entity
@Table(name="NewProductDetails")
public class Products{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "p_id")
private int productId;
@Size(max=65)
@Column(name = "p_name")
private String name;
@Column(name = "p_price")
private int price;
@OneToMany(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "myproduct")
public Set<Customer> c;
public Products() {
}
public Products(String p_name, int p_price) {
this.name = p_name;
this.price = p_price;
}
public long getproductId() {
return productId;
}
public void setproductId(int id) {
this.productId = id;
}
public void setPName(String p_name) {
this.name = p_name;
}
public String getPName() {
return this.name;
}
public void setPrice(int p_price ) {
this.price = p_price ;
}
public int getPrice() {
return this.price;
}
}
ProductController.java
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class ProductController {
@Autowired
ProductRepository productRepository;
@GetMapping("/product")
public List<Products> getAllProducts(){
System.out.println("Get All the product .... ");
List<Products> products = new ArrayList<>();
productRepository.findAll().forEach(products :: add);
return products;
}
@GetMapping("/product/{id}")
public ResponseEntity<Products> findByProductId(@PathVariable("p_id") Long p_id ){
Optional<Products> prod_ = productRepository.findByProductId(p_id);
return ResponseEntity.ok(prod_.get());
}
@PostMapping(value = "/product")
public Products postProducts(@RequestBody Products product) {
Products _product = productRepository.save(new Products(product.getPName(), product.getPrice() ));
return _product;
}
}
ProductRepository.java
@Repository
public interface ProductRepository extends CrudRepository<Products, Long>{
Optional<Products> findByProductId(Long p_id);
}
CustomerRepository.java
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController {
@Autowired
CustomerRepository repository;
@GetMapping("/customer")
public List<Customer> getAllCustomers() {
System.out.println("HOOHAAH Get all Customers...");
List<Customer> customers = new ArrayList<>();
repository.findAll().forEach(customers::add);
System.out.println(customers);
return customers;
}
@GetMapping("/customer/{id}")
public ResponseEntity<Customer> findById(@PathVariable("id") Long Id){
Optional<Customer> cust_ = repository.findById(Id);
return ResponseEntity.ok(cust_.get());
}
@PostMapping(value = "/customer")
public Customer postCustomer(@RequestBody Customer customer) {
Customer _customer = repository.save(new Customer(customer.getName(), customer.getAge(), customer.getProduct()));
return _customer;
}
@DeleteMapping("/customer/{id}")
public ResponseEntity<String> deleteCustomer(@PathVariable("id") long id) {
System.out.println("Delete Customer with ID = " + id + "...");
repository.deleteById(id);
return new ResponseEntity<>("Customer has been deleted!", HttpStatus.OK);
}
@DeleteMapping("/customer")
public ResponseEntity<String> deleteAllCustomers() {
System.out.println("Delete All Customers...");
repository.deleteAll();
return new ResponseEntity<>("All customers have been deleted!", HttpStatus.OK);
}
@GetMapping(value = "customer/age/{age}")
public List<Customer> findByAge(@PathVariable int age) {
List<Customer> customers = repository.findByAge(age);
return customers;
}
@PutMapping("/customer/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable("id") long id, @RequestBody Customer customer) {
System.out.println("Update Customer with ID = " + id + "...");
Optional<Customer> customerData = repository.findById(id);
if (customerData.isPresent()) {
Customer _customer = customerData.get();
_customer.setName(customer.getName());
_customer.setAge(customer.getAge());
_customer.setActive(customer.isActive());
return new ResponseEntity<>(repository.save(_customer), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
主要
@SpringBootApplication
public class SpringRestMySqlApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringRestMySqlApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
//Create Product Instance
Products prods = new Products();
//Create Customer instance
Customer custs = new Customer();
}
}
浏览器显示错误
SyntaxError: JSON 在 JSON.parse 输入的意外结束 () 在 XMLHttpRequest.onLoad
"[{"id":6,"name":"Asma","age":18,"active":true,"myproduct":{"productId":2,"price":4,"c":[{"id":6,"name":"Asma","age":18,"active":true,"myproduct":{"productId":2,"price":4,"c":[{"id":6,.....
服务器日志错误
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
........
我在每个实体(产品和客户)中使用 @JsonIgnoreProperties() 解决了错误,循环依赖消失了。
我在这个很棒的博客上找到了解决 JSON 中的递归循环依赖的帮助,方法是 方法 # 3
http://springquay.blogspot.com/2016/01/new-approach-to-solve-json-recursive.html
我已经处理了几个小时,我似乎可以解决这个问题。我有两个实体 Products 和 Customer,其中一个客户可以拥有一个产品,而一个产品可能有很多客户。在我的 SQL 服务器管理工作室中,产品 table 的主键作为外键存在于客户 table.
中我在下面的代码中展示了两个实体。问题是客户 "c" 被递归附加到 "myproducts",这是 JSON 中的 mappedBy 属性,当我在浏览器 window 上查看控制台时显示。 (请参阅下面错误中的嵌套对象 "myproducts" 和 "c")
我正在使用 GET 方法 API 在屏幕上显示客户。
Products.java
@Entity
@Table(name="NewProductDetails")
public class Products{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "p_id")
private int productId;
@Size(max=65)
@Column(name = "p_name")
private String name;
@Column(name = "p_price")
private int price;
@OneToMany(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "myproduct")
public Set<Customer> c;
public Products() {
}
public Products(String p_name, int p_price) {
this.name = p_name;
this.price = p_price;
}
public long getproductId() {
return productId;
}
public void setproductId(int id) {
this.productId = id;
}
public void setPName(String p_name) {
this.name = p_name;
}
public String getPName() {
return this.name;
}
public void setPrice(int p_price ) {
this.price = p_price ;
}
public int getPrice() {
return this.price;
}
}
ProductController.java
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class ProductController {
@Autowired
ProductRepository productRepository;
@GetMapping("/product")
public List<Products> getAllProducts(){
System.out.println("Get All the product .... ");
List<Products> products = new ArrayList<>();
productRepository.findAll().forEach(products :: add);
return products;
}
@GetMapping("/product/{id}")
public ResponseEntity<Products> findByProductId(@PathVariable("p_id") Long p_id ){
Optional<Products> prod_ = productRepository.findByProductId(p_id);
return ResponseEntity.ok(prod_.get());
}
@PostMapping(value = "/product")
public Products postProducts(@RequestBody Products product) {
Products _product = productRepository.save(new Products(product.getPName(), product.getPrice() ));
return _product;
}
}
ProductRepository.java
@Repository
public interface ProductRepository extends CrudRepository<Products, Long>{
Optional<Products> findByProductId(Long p_id);
}
CustomerRepository.java
@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api")
public class CustomerController {
@Autowired
CustomerRepository repository;
@GetMapping("/customer")
public List<Customer> getAllCustomers() {
System.out.println("HOOHAAH Get all Customers...");
List<Customer> customers = new ArrayList<>();
repository.findAll().forEach(customers::add);
System.out.println(customers);
return customers;
}
@GetMapping("/customer/{id}")
public ResponseEntity<Customer> findById(@PathVariable("id") Long Id){
Optional<Customer> cust_ = repository.findById(Id);
return ResponseEntity.ok(cust_.get());
}
@PostMapping(value = "/customer")
public Customer postCustomer(@RequestBody Customer customer) {
Customer _customer = repository.save(new Customer(customer.getName(), customer.getAge(), customer.getProduct()));
return _customer;
}
@DeleteMapping("/customer/{id}")
public ResponseEntity<String> deleteCustomer(@PathVariable("id") long id) {
System.out.println("Delete Customer with ID = " + id + "...");
repository.deleteById(id);
return new ResponseEntity<>("Customer has been deleted!", HttpStatus.OK);
}
@DeleteMapping("/customer")
public ResponseEntity<String> deleteAllCustomers() {
System.out.println("Delete All Customers...");
repository.deleteAll();
return new ResponseEntity<>("All customers have been deleted!", HttpStatus.OK);
}
@GetMapping(value = "customer/age/{age}")
public List<Customer> findByAge(@PathVariable int age) {
List<Customer> customers = repository.findByAge(age);
return customers;
}
@PutMapping("/customer/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable("id") long id, @RequestBody Customer customer) {
System.out.println("Update Customer with ID = " + id + "...");
Optional<Customer> customerData = repository.findById(id);
if (customerData.isPresent()) {
Customer _customer = customerData.get();
_customer.setName(customer.getName());
_customer.setAge(customer.getAge());
_customer.setActive(customer.isActive());
return new ResponseEntity<>(repository.save(_customer), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
主要
@SpringBootApplication
public class SpringRestMySqlApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SpringRestMySqlApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
//Create Product Instance
Products prods = new Products();
//Create Customer instance
Customer custs = new Customer();
}
}
浏览器显示错误 SyntaxError: JSON 在 JSON.parse 输入的意外结束 () 在 XMLHttpRequest.onLoad
"[{"id":6,"name":"Asma","age":18,"active":true,"myproduct":{"productId":2,"price":4,"c":[{"id":6,"name":"Asma","age":18,"active":true,"myproduct":{"productId":2,"price":4,"c":[{"id":6,.....
服务器日志错误
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107) ~[jackson-databind-2.9.6.jar:2.9.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.9.6.jar:2.9.6]
........
我在每个实体(产品和客户)中使用 @JsonIgnoreProperties() 解决了错误,循环依赖消失了。
我在这个很棒的博客上找到了解决 JSON 中的递归循环依赖的帮助,方法是 方法 # 3 http://springquay.blogspot.com/2016/01/new-approach-to-solve-json-recursive.html