Home > database >  springboot: Parameter 0 of method addProduct required a bean of type 'java.lang.String' th
springboot: Parameter 0 of method addProduct required a bean of type 'java.lang.String' th

Time:02-20

I am new to spring boot and I want to create an application that inserts value to a database

I have create the services, repository, configuration and controller

But I am getting this error

Parameter 0 of method addProduct in com.oshabz.springboot.repositories.ProductRepository required a bean of type 'java.lang.String' that could not be found.

Action:

Consider defining a bean of type 'java.lang.String' in your configuration.

My code

@Repository
public class ProductRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    
    @Bean
    public void addProduct(String name) {
        String sql = "INSERT INTO product VALUES (NULL, ?)";
        jdbcTemplate.update(sql, name);
    }
}


@Service
public class ProductService {
    
    @Autowired
    ProductRepository productRepository;
    
    
    public void addProduct(String name) {
        productRepository.addProduct(name);
    }
}

    
@RestController
@RequestMapping(path="product")
public class ProductController {
    
    @Autowired
    ProductService productService;
    
    @PostMapping(path="/add/{name}")
    public void addProduct(@PathVariable String name) {
        productService.addProduct(name);
    }
}

CodePudding user response:

Remove the @Bean annotation from the addProduct method. That annotation is supposed to be used in configuration classes on methods, which create bean instances.

  • Related