Search code examples
javaspringboot

How to pass the value of an @value annotation to the controller in SpringBoot


I have a spring in which I use the dependencies of: spring-boot-starter-web v-2.7.3, spring-boot-devtools and spring-boot-starter-thymeleaf my purpose is to send an "Invoice" class with the dependencies of a client class and an ItemFactura class to the controller, with the @value annotation I add an environment variable located in the application.properties:

factura.descripcion: Deporte

I add it with @value to my java invoice class:

@Component
public class Factura {

@Value("${factura.descripcion}")
private String descripcion;

@Autowired
private Cliente cliente; 

private List<ItemFactura> items; 

/**
 * @return String return the description
 */
public String getDescription() {
    return descripcion;
}

/**
 * @param descripcion the description to set
 */
public void setDescription(String descripcion) {
    this.descripcion = descripcion;
}

/**
 * @return Cliente return the cliente
 */
public Cliente getCliente() {
    return cliente;
}

/**
 * @param cliente the cliente to set
 */
public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

/**
 * @return List<ItemFactura> return the items
 */
public List<ItemFactura> getItems() {
    return items;
}

/**
 * @param items the items to set
 */
public void setItems(List<ItemFactura> items) {
    this.items = items;
}

}

Después de esto inyecto la clase Factura en el controlador:

@Controller
@RequestMapping("/Factura")
public class FacturaController {

@Autowired
private Factura factura; 

@GetMapping("/ver")
public String verFactura(Model model){
    model.addAttribute("factura", factura);
    model.addAttribute("Titulo_factura", "Esto es el título de una factura");

    return "index";
    
}

}

Y por último la llamó en mi index.html:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Facturas</title>
</head>
<body>
<h1>Aquí se ven las facturas</h1>

<h1 th:text="${Titulo_factura}"></h1>
<h1 th:text="${factura.descripcion}"></h1>



</body>
</html>

When I lift again and enter my route I get the error: Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "invoice.description" (template: "ver-invoice" - line 13, col 9) and **Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'description' cannot be found on object of type 'com.nodependencyinjection.demo.Domain.Factura' - maybe not public or not valid? **

Mi primera reacción fue cambiar private descripcion por public, pero lo que intento es realizar un encapsulamiento del objeto, la única opción que veo para este error es que mi clase com.nodependencyinjection.demo.Domain.Factura y mi com.nodependencyinjection.demo.Controllers están en paquetes diferentes, o que para utilizar las variables de entorno haya que agregar una configuración especial en application.properties, la cual desconozco, cualquier ayuda se agradece de antemano

pddt: when I add "factura" only in the html the page loads correctly and I get the hash of the class.

this confuses me... html image


Solution

  • Property is descripcion so you need a getter getDescripcion() and not getDescription()