PostMapping逻辑错误

huangapple 未分类评论44阅读模式
英文:

PostMapping Logic errors

问题

我在我的服务中有一个方法,可以将水果保存在我所有的商店中。它在主方法中运行良好,但在使用Postman时却不行。当我使用Postman进行测试时,它只创建了一个水果实例。

*服务代码*

    @Override
	public Fruit addFruit(Fruit f) {
       Iterable<Store> stores = storeService.getAllStores();
       for (Store store : stores) {
    	 Fruit fruit = new Fruit();
    	 fruit.setStore(store);
    	 fruit.setAlert(f.getAlert());
    	 fruit.setCategory(f.getCategory());
    	 fruit.setDesignation(f.getDesignation());
    	 fruit.setLign(f.getLign());
    	 fruit.setReference(f.getReference());
    	 fruit.setStock(f.getStock());
    	 f.setStore(store);
    	 fruitRepository.save(fruit);
	     }
     
	  return f;    
	}
*Postman JSON*

    {
    "reference": "reference1",
    "category": "Citrus",
    "designation": "Orange and Lemon",
    "lign": 20,
    "stock": 60
    }
英文:

I have a method in my service that can save a fruit in all the stores that I have. It works well in main method, but not with Postman. When I test with Postman it creates just one instance of the fruit.

Service code

@Override
public Fruit addFruit(Fruit f) {
   Iterable&lt;Store&gt; stores=storeService.getAllStores();
   for (Store store : stores) {
	 Fruit fruit = new Fruit();
	 fruit.setStore(store);
	 fruit.setAlert(f.getAlert());
	 fruit.setCategory(f.getCategory());
	 fruit.setDesignation(f.getDesignation());
	 fruit.setLign(f.getLign());
	 fruit.setReference(f.getReference());
	 fruit.setStock(f.getStock());
	 f.setStore(store);
	 fruitRepository.save(fruit);
     }
 
  return f;    
}

*postman Json *

{
&quot;reference&quot;:&quot;reference1&quot;,
&quot;category&quot;:&quot;Citrus&quot;,
&quot;designation&quot;:&quot;Orange and Lemon&quot;,
&quot;lign&quot;:20,
&quot;stock&quot;:60
}

答案1

得分: 0

getAllstore services

     @Override
	 public Iterable<Store> getAllStores() {
     return storeRepository.findAll();
	 }

Controller

    @Path("/fruits")
    @POST
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    @Produces(MediaType.APPLICATION_JSON_VALUE)
	public Fruit addProduct(@RequestBody Fruit f) {
		return fruitService.addFruit(f);
	}
英文:

getAllstore services

 @Override
 public Iterable&lt;Store&gt; getAllStores() {
 return storeRepository.findAll();
 }

Controller

@Path(&quot;/fruits&quot;)
@POST
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public Fruit addProduit(@RequestBody Fruit f) {
	return fruitService.addFruit(f);
}

答案2

得分: 0

Service:

@Override 
public Set<Fruit> addFruit(Fruit f) {
  Iterable<Store> stores = storeService.getAllStores();
  Set<Fruit> createdFruits = new HashSet<>(stores.size());
  for (Store store : stores) { 
    Fruit fruit = new Fruit();
    fruit.setStore(store);
    fruit.setAlert(f.getAlert());
    fruit.setCategory(f.getCategory());
    fruit.setDesignation(f.getDesignation());
    fruit.setLign(f.getLign());
    fruit.setReference(f.getReference());
    fruit.setStock(f.getStock());
    createdFruits.add(fruitRepository.save(fruit)); 
  } 
  return createdFruits; 
}

Controller:

@Path("/fruits") 
@POST @Consumes(MediaType.APPLICATION_JSON_VALUE) @Produces(MediaType.APPLICATION_JSON_VALUE)
public Set<Fruit> addProduit(@RequestBody Fruit f) {
  return fruitService.addFruit(f);
}
英文:

You need to return a collection of Fruit instead of one Fruit in the Controller and also in the Service, so you can return back what you have created in the database, and you can also check the result in Postman.

Service:

@Override 
public Set&lt;Fruit&gt; addFruit(Fruit f) {
  Iterable&lt;Store&gt; stores=storeService.getAllStores();
  Set&lt;Fruit&gt; createdFruits = new HashSet&lt;&gt;(  stores.size() )
  for (Store store : stores) { 
    Fruit fruit = new Fruit();
    fruit.setStore(store);
    fruit.setAlert(f.getAlert());
    fruit.setCategory(f.getCategory());
    fruit.setDesignation(f.getDesignation());
    fruit.setLign(f.getLign());
    fruit.setReference(f.getReference());
    fruit.setStock(f.getStock());
 //f.setStore(store);  // You don&#39;t need this 
    createdFruits.add( fruitRepository.save(fruit) ); 
  } 

  return createdFruits; 
}

Controller:

@Path(&quot;/fruits&quot;) 
@POST @Consumes(MediaType.APPLICATION_JSON_VALUE) @Produces(MediaType.APPLICATION_JSON_VALUE)
 public Set&lt;Fruit&gt; addProduit(@RequestBody Fruit f) {
   return fruitService.addFruit(f);
}

huangapple
  • 本文由 发表于 2020年7月26日 18:24:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/63098881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定