- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经研究了几个小时,似乎可以解决这个问题。我有两个实体产品和客户,其中一个客户可以拥有一种产品,而一个产品可能有多个客户。在我的 SQL SERVER Management studio 中,Product 表的主键作为 Customer 表中的外键。
我在下面的代码中展示了这两个实体。问题在于,客户“c”被递归地附加到“myproducts”,这是当我在浏览器窗口上检查控制台时显示的 JSON 中的mappedBy 属性。 (请参阅下面错误中的嵌套对象“myproducts”和“c”)
我正在使用 GET 方法 API 在屏幕上显示客户。
<小时/>产品.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();
}
}
<小时/>
浏览器中显示错误语法错误:XMLHttpRequest.onLoad 处的 JSON.parse () 处的 JSON 输入意外结束
"[{"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() 解决了错误,并且循环依赖消失了。
我在这个很棒的博客上找到了帮助,通过遵循它的方法#3来解决 JSON 中的递归循环依赖关系 http://springquay.blogspot.com/2016/01/new-approach-to-solve-json-recursive.html
关于java - 递归 JSON 输入导致 SyntaxError : Unexpected end of JSON input at JSON. 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58298798/
一旦在 qconsole Marklogic 中运行以下代码,我就会遇到以下错误 XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax err
我已经在我的包中编写了这个函数。 def partitionIntoDays(ls, number, lookupKey=None): ''' Partitions the location
我只是一个 android 初学者,我已经安装了 Android Studio(版本是 1.0.2),并创建了一个新的空白应用程序,按照名为“构建你的第一个应用程序”的官方教程,我学习到这个页面' h
这只是前几天工作,但我刚刚将我的代码更新到运行乘客 2.2.4 的审查服务器,而我的 2.3.4 rails 应用程序现在无法在那个盒子上启动。 乘客报告: Passenger encountered
我正在尝试使用带有 Angular 2的整页, 将其导入我的 app.module.ts 时出现以下错误。 "(SystemJS) Unexpected token ) at Obje
TFS2015 vNext 构建失败并出现记录器错误(下面附有错误消息)。根据我的调查,这看起来与 CentralLogger - "Microsoft.TeamFoundation.Distribu
计算机科学学校项目。我需要编写一个程序,其中用户声明数组的大小,然后以数字、非递减顺序填充数组,然后声明一个值 x。然后将 X 分配到适当的位置,以便整个数组按数字、非递减顺序排列。然后输出该数组。
在这 2 个方法中,inspect1 显示编译错误“Unexpected bound”而 inspect2 工作正常,为什么? public void inspect1(List u){ S
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在尝试运行以下代码,但遇到了“此时意外”错误。 (echo COPY (SELECT ta.colA as name, ta.colB as user_e, ta.colC as user_n,
我有以下查询: select u.UserName, count(*) as total from Voting v join User u using (UserID) where unique (
我们有以下查询在 MSSQL 中完美运行但在 MySQL 中无法运行: select CONVERT(datetime, dateVal) as DateOccurred, itemID, COUNT
我的代码中存在缩进错误问题。它看起来是正确的...有人能指出我做错了什么吗?我的查询行不断收到错误。 def invoice_details(myDeliveryID): conn = pym
我有以下代码: int a , b , sum; cin>>a>>b; sum=a+b; cout>a>>b>>c; cout<
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我在一个批处理文件上运行这个命令: for %I in (*.txt *.doc) do copy %I c:\test2 ...它不断返回: I was unexpected at this tim
创建查询时出现错误: 'from' unexpected 我的代码如下: @Override public Admin findByAdmin(Admin admin) {
我正在尝试运行此 python 代码,但我不断收到错误消息“意外缩进”。我不确定怎么了。间距似乎很好。有什么想法吗? services = ['Service1'] for service in
我在名为“circular_dependency”的目录中有一些 python 文件: 导入文件_1.py: from circular_dependency.import_file_2 import
我正在尝试使用 gcc 编译代码并运行可执行文件,但它抛出错误: gcc somefile.c -o somefile 编译成功。但是,当我尝试执行它时: $sh somefile 它导致:语法错误:
我是一名优秀的程序员,十分优秀!