Home > database >  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 

Time:05-24

Hi I'm new to SpringBoot and I keep getting this error when I try to run the application along with " Exception encountered during context initialization - cancelling refresh attempt". This is my controller class(orderController)

`package com.example.demo.controller;

 import com.example.demo.model.Order;
 import com.example.demo.repository.OrderRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;


 import java.util.List;

 @RestController
 @RequestMapping("api/")
 public class OrderController {

@Autowired
private final OrderRepository orderRepository;

public OrderController(OrderRepository orderRepository) {
    this.orderRepository = orderRepository;
}

@GetMapping("/Order")
public ResponseEntity<List<Order>> getAllOrders() {
    return ResponseEntity.ok(this.orderRepository.findAll());

}
@PostMapping("/Order")
public ResponseEntity<Order> createOrders(@RequestBody Order orderRequest) {
    Order order=new Order();
    order.setId(orderRequest.getId());
    order.setSellerId(orderRequest.getSellerId());
    order.setAddress(orderRequest.getAddress());

    return ResponseEntity.ok(this.orderRepository.save(order));

}

}

`

CodePudding user response:

Remove the @Autowired annotation at the field and add at the constructor level.

package com.example.demo.controller;

 import com.example.demo.model.Order;
 import com.example.demo.repository.OrderRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;


 import java.util.List;

 @RestController
 @RequestMapping("api/")
 public class OrderController {

//@Autowired -remove from here
private final OrderRepository orderRepository;

@Autowired // add here
public OrderController(OrderRepository orderRepository) {
    this.orderRepository = orderRepository;
}

@GetMapping("/Order")
public ResponseEntity<List<Order>> getAllOrders() {
    return ResponseEntity.ok(this.orderRepository.findAll());

}
@PostMapping("/Order")
public ResponseEntity<Order> createOrders(@RequestBody Order orderRequest) {
    Order order=new Order();
    order.setId(orderRequest.getId());
    order.setSellerId(orderRequest.getSellerId());
    order.setAddress(orderRequest.getAddress());

    return ResponseEntity.ok(this.orderRepository.save(order));

}

This should work

CodePudding user response:

you can use the dependency called Lombok... very useful:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>{Version what you need}</version>
    </dependency>

Lombok will create with this annotation a constuctor by picking just the final fields.

you can do after adding this:

@RestController
@RequiredArgsConstructor
public class OrderController {


  private final OrderRepository orderRepository;


  @GetMapping("/Order")
  public ResponseEntity<List<Order>> getAllOrders() {
    return ResponseEntity.ok(this.orderRepository.findAll());

  }

  @PostMapping("/Order")
  public ResponseEntity<Order> createOrders(@RequestBody Order 
  orderRequest) {
     Order order=new Order();
     order.setId(orderRequest.getId());
     order.setSellerId(orderRequest.getSellerId());
     order.setAddress(orderRequest.getAddress());

     return ResponseEntity.ok(this.orderRepository.save(order));

  }}
  • Related