当我在ProductRestService中调用SaleRestService时,如下面的代码所示,我收到org.springframework.beans.factory.UnsatisfiedDependencyException。
我该怎么做?
@RestController
@CrossOrigin("*")
public class ProductRestService {
@Autowired
private ProductRepository productRepository;
@Autowired
public SaleRestService saleRestService ;
@RequestMapping(value="/productQuatityMinusOne", method=RequestMethod.GET)
@ResponseBody
public void ProductQuatityMinusOne(@RequestParam(name="id") Long id){
Product p = productRepository.findProductById(id);
double salePrice = p.getPrice();
Date now = new java.util.Date();
Sale s = new Sale(id,salePrice,now);
saleRestService .saveOneSale(s);
p.setId(id);
int q = p.getQuantity()-1;
p.setQuantity(q);
productRepository.save(p);
}
}
@RestController
@CrossOrigin("*")
public class SaleRestService {
@Autowired
private SaleRepository saleRepository;
//Save one sale
@RequestMapping(value="/saveOneSale", method=RequestMethod.POST)
@ResponseBody
public Sale saveOneSale(@RequestBody Sale s){
return saleRepository.save(s);
}
}
答案 0 :(得分:1)
您不应相互调用控制器。
两种解决方案:
return "redirect:/saveOneSale";
这样的弹簧引导重定向,将ProductRestService中ProductQuatityMinusOne的http调用重定向到SaleRestService中的saveOneSale,但是我不知道这样是否可行,因为您将重定向到POST处理程序。我个人会做第一个解决方案:
一切都应该正常工作(TM)