In a #ConfigurationProperties bean, I could map the customers property of the application.yml file below to List<Customer>, but I would like to instead map it to a Jackson JsonNode. Is this possible? If so, how?
shop:
name: "Sam's Bikes"
customers:
- name: Lucy
age: 26
- name: James
age: 24
This is what I'd like to achieve:
#ConfigurationProperties("shop")
public class ShopProperties() {
private String name;
private JsonNode customers;
}
Since customers in application.yml are in Array of objects format, i would recomend either to collect them into List<Customer> or List<Map<String,Object>> using below code, #Data is lombok annotation, if you are not using lombok add getters and setters
#ConfigurationProperties("shop")
#Data
#Configuration
public class TestConfig {
private String name;
private List<Map<String,Object>> customers; //[{name=Lucy, age=26}, {name=James, age=24}]
}
Related
I have the following configuration in my Spring boot's application.yml file:
project:
country-properties:
france:
capital: paris
population: 60
And I have the the properties class : CountryProperties :
#Getter
#AllArgsConstructor
#ConstructorBinding
#ConfigurationProperties(prefix="project.country-properties")
public class CountryProperties {
private Map<String, CountryData> countryProperties;
#Getter
#Setter
public static class CountryData {
private String capital;
private Integer population;
}
However my CountryProperties is always null, and it's because of a failed mapping with the CountryData object.
Any ideas what is wrong with what I wrote?
You have the annotation #ConstructorBinding. This annotation tells Spring to look for a constructor in your class that has parameters corresponding to your configuration properties, and then will bind the properties.
What you are missing is:
public CountryProperties(Map<String, CountryData> countryProperties) {
this.countryProperties = countryProperties;
}
Update:
After inspecting your code again, it looks like you aren't mapping the configuration correctly to the instance field. Please update your #ConfigurationProperties(prefix="project.country-properties") to #ConfigurationProperties(prefix="project").
Also replace the #ConstructorBinding with #Configuration.
I have a yaml file with a structure
section:
subsections:
subsectionName1:
param1: value1
param2: value2
subsectionName2:
param1: value1
param2: value2
I need to parse these properties as List<CustomClass>
#ConfigurationProperties(prefix = "section.subsections")
#Configuration
public class CustomClass {
private final String subsectionName; //the problem is there
private String param1;
private String param2;
//Constructors, getters, setters
How can I do this in Spring?
I see that this works in Micronaut this way:
import io.micronaut.context.annotation.Context;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import lombok.EqualsAndHashCode;
#Context
#EachProperty("section.subsections")
#EqualsAndHashCode
public class CustomClass {
private final String subsectionName;
private String param1;
private String param2;
public CustomClass(#Parameter String subsectionName) {
this.subsectionName = subsectionName;
}
//getters, setters...
But I didn't manage to find something similar to io.micronaut.context.annotation.Parameter in Spring.
I need this to use the shared util class which accepts List as a parameter.
Are there any ideas on how to do this?
I really appreciate any help you can provide.
Maybe you can try something like that :
section:
subsections:
subsectionName1: value1,value2
And in your code you need to parse this using #Value annotation and split function of Strings
#Value("#{'${section.subsections:}'.split(',')}")
Set<String> subsections
In that case I use set, but you cant use list if you allow repeated values.
Here is an example of my problem. when no value is supplied to default-name in yml file. #DefaultValue should step in and fill with "Name". However, is not how it behaves. An empty string is assigned to defaultName
application.yml:
account:
default-name:
class:
#ConstructorBinding
#ConfigurationProperties(prefix = "account")
public class Account {
private final String defaultName;
public Account(#DefaultValue("Name") String defaultName) {
this.defaultName = defaultName;
}
..
..
}
When I send request http://localhost:8080/pets My server response 404!
The code on github: https://github.com/Teemitze/petstore
I build war file. Version spring 2.2.6.RELEASE
#Controller
#RequestMapping("/pets")
public class PetsController {
#Autowired
PetRepository petRepository;
#PostMapping("/addPet")
public void addPet(Pet pet) {
petRepository.save(pet);
}
#GetMapping
#ModelAttribute
public String pets(Model model) {
List<Pet> petList = new ArrayList<>();
petList.add(getPet());
petList.add(getPet());
petList.add(getPet());
model.addAttribute("pets", petList);
return "allPets";
}
public Pet getPet() {
Pet pet = new Pet();
pet.setId(1L);
pet.setName("Мурзик");
pet.setPrice(100);
pet.setBirthday(Date.valueOf("2019-12-12"));
pet.setSex("М");
return pet;
}
}
I checked out your code and found a few issues.
1) Package structure
Move controller, dto, repo packages to the main package (com.petstore)
Since the main application is inside the (com.petstore) package and the controller is outside the package, so it fails to scan the class.
2) Use annotation #Entity for the Pet entity class with #Id for the id property
3) Remove #ModelAttribute from pets() method since you are not binding any method parameter.
After this, I see the /pets
SpringBoot project requires define some configuration conventions that need to be follow in order to start a minimum application.
Some points you have to consider when you want to start a spring boot application.
For example:
Your SpringBootApplication(PetstoreApplication) class should be in the directory level above your other packages so that it can scan all classes.
If you want to use SpringData JPA you have to manage your model class
#Data
#Entity
public class Pet {
#Id
private long id;
private String name;
private String sex;
private Date birthday;
private byte[] photo;
private int price;
}
because it is handled by respository
public interface PetRepository extends CrudRepository<Pet, Long>
Need minimum configuration for Thymeleaf https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html
You are making a GET request for a resource "/pets" so no need #ModelAttribute in get mapping method
#GetMapping()
public String allPets(Model model) {
Make sure your html files is under resources/templates directory.
Check out the reference docs
spring mvc
spring data jpa
I am working with Hibernate 4+ Spring MVC + Spring Data with JPA annotations:
#Entity
public class ClassOne implements Serializable{
......
#OneToMany(mappedBy = "mapper", fetch=FetchType.LAZY)
private Set<ClassTwo> element = new HashSet<ClassTwo>(0);
//more fields
//getters and setters
//equals and hashcode
}
#Entity
public class ClassTwo implements Serializable{
......
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name = "CEN_CEN_CODIGO", nullable = false)
private ClassOne classOne;
//more fields
//getters and setters
//equals and hashcode
}
public interface ClassOneRepository extends CrudRepository<ClassOne, Long> {
#Override
#Query("select c from ClassOne c")
public List<ClassOne> findAll();
}
#Service
public class ClassOneService {
#Autowired
private ClassOneRepository classOneRepository;
#Transactional(readOnly=true)
public List<ClassOne> findAll() {
return classOneRepository.findAll();
}
}
And finally I call thie service from my #Controller
#Autowired
ClassOneService classOneService;
I expect results ONLY from ClassOne but retrieving the JOIN values with ClassTwo and all the database tree associate. Is it possible to get only values for ONE table using this schema? Is it a cache problem or Fetching not LAZY?
EDIT: I added the relatioship between two classes
Thank you
You must have the following anotation above your Set<ClassTwo> or its getter:
#OneToMany(fetch = FetchType.LAZY, ...)
See http://docs.oracle.com/javaee/7/api/javax/persistence/OneToMany.html#fetch()
It seems to be that simple "SELECT *" JPA query returns all eagerly fetched fields for the entity.
Please refer to: JPA - Force Lazy loading for only a given query
and http://forcedotcom.github.io/java-sdk/jpa-queries.
So my solution would be to use SessionFactory to get current session and then use "classic" method
return getCurrentSession().createCriteria(persistentClass).list();
Another possible way is to create let's say a POJO object without Set which uses the same table as ClassOne.
I've just added #Lazy for each #ManyToOne and #OneToMany relationship. It seems that Spring Data needs Spring annotations but I supposed that just was necessary to add fetch = FetchType.LAZY. No more Eager behaviours ;).
Thanks for your responses