Ad

Springboot: Parameter 0 Of Method AddProduct Required A Bean Of Type 'java.lang.String' That Could Not Be Found

- 1 answer

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);
    }
}
Ad

Answer

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

Ad
source: stackoverflow.com
Ad