Spring configuration metadata Map missing description - spring

I'm using the following maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
I have the following configuration class:
#Configuration
#ConfigurationProperties(prefix = "example")
public class MyProperty {
/**
* myVariable documentation.
*/
private Map<String, Integer> myVariable;
/**
* myVariable2 documentation.
*/
private String myVariable2;
My generated spring-configuration-metadata.json
{
"name": "example",
"type": "java.lang.String",
"description": "myVariable2 documentation.",
"sourceType": "path.MyProperty"
},
{
"name": "example",
"type": "java.util.Map<java.lang.String,java.lang.Integer>",
"sourceType": "path.MyProperty"
}
For the Map the description is missing. Any ideas why?

Related

mongodb reactive spring boot DBRef resolution is not supported

I wrote this programe spring boot mongodb reactive
#SpringBootApplication
public class ReactitveMongoDbApplication {
public static void main(String[] args) {
SpringApplication.run(ReactitveMongoDbApplication.class, args);
}
#Bean
CommandLineRunner ok(SoscieteRepository sos, TransactionRepository trs) {
return a -> {
List<Sosciete> ls = List.of(new Sosciete("SG", "sosciete general", 1235.22),
new Sosciete("AB", "AIR BOLL", 478.36), new Sosciete("TO", "TOYOTA", 458.24));
trs.deleteAll().subscribe(null, null, () -> {
sos.deleteAll().subscribe(null, null, () -> {
ls.forEach(t -> sos.save(t).subscribe(so -> {
System.out.println(so);
for (int i = 0; i < 10; i++) {
Transaction tr = new Transaction();
tr.setDate(Instant.now());
tr.setSosciete(so);
double x = 1 + ((Math.random() * 12) - 6) / 100;
tr.setPrice(so.getPrice() * x);
trs.save(tr).subscribe(ts -> {
System.out.println(ts);
});
}
}));
});
});
System.out.println("done !");
};
}
}
interface SoscieteRepository extends ReactiveMongoRepository<Sosciete, String> {
}
#Document
#Data
#AllArgsConstructor
#NoArgsConstructor
class Sosciete {
#Id
private String id;
private String name;
private double price;
}
interface TransactionRepository extends ReactiveMongoRepository<Transaction, String> {
}
#Document
#Data
#AllArgsConstructor
#NoArgsConstructor
class Transaction {
#Id
private String id;
private Instant date;
private double price;
#DBRef(db = "sosciete", lazy = true)
private Sosciete sosciete;
}
#RestController
class ReactiveRestController {
#Autowired
private SoscieteRepository sos;
#Autowired
private TransactionRepository trans;
#GetMapping(value = "/soscietes")
public Flux<Sosciete> getAllSc() {
return sos.findAll();
}
#GetMapping(value = "/transactions")
public Flux<Transaction> getAllTr() {
return trans.findAll();
}
}
the dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
aplication.prop file:
spring.data.mongodb.database=webflux
spring.data.mongodb.port=27017
server.port=3000
my code works fine in this link :
http://localhost:3000/soscietes
but in this link:
http://localhost:3000/transactions
the code throws the following error:
java.lang.UnsupportedOperationException: DBRef resolution is not supported!
it's the DBRef annotation that is cause for this error,
it does not work at all. It throws the following exception.
can we add such a configuration to make it work?
thank you in advance
I got the same issue with Spring boot version : 2.7.0
And i changed #DBRef to #DocumentReference and it works
I found the solution:
i modified the spring boot parent version in pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath />
</parent>
to 2.1.2.RELEASE

Spring Kafka Consumer consumed message as LinkedHashMap hence automatically converting BigDecimal to double

I am using annotation based spring kafka listener to consume the kafka messages, and code is as below
Consuming Employee Object
Class Employee{
private String name;
private String address;
private Object account;
//getters
//setters
}
Account object decides on runtime whether it's Saving Account or Current Account etc.
Class SavingAcc{
private BigDecimal balance;
}
Class CurrentAcc{
private BigDecimal balance;
private BigDecimal limit;
}
Saving & Current Account having BigDecimal Fields to store balance.
Hence while sending Employee object from Kafka producer, all the fields are correctly mapped and appears in correct format of BigDecimal, etc.
But while consuming the Employee object in another service, account object is appearing as LinkedHashMap and BigDecimal fields are converted to Double. which is causing issues.
As per my understanding, the main reason can be as
a) Declaration of account as Object type instead of specific type
b) Or the deserializer should be provided more specifically. [I have already give Employee.class as type to kafka receiver deserializer, so Employee fields are correctly mapped but account fields wrong].
#Bean
public ConsumerFactory<String, Employee> consumerFactory(){
return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(), new JsonDeserializer<>(Employee.class));
}
Need help on how to map or how to get the account fields properly deserialize.
Use Generics and a custom JavaType method.
Class Employee<T> {
private String name;
private String address;
private T account;
//getters
//setters
}
JavaType withCurrent = TypeFactory.defaultInstance().constructParametricType(Employee.class, CurrentAcc.class);
JavaType withSaving = TypeFactory.defaultInstance().constructParametricType(Employee.class, SavingAcc.class);
public static JavaType determineType(String topic, byte[] data, Headers headers) {
// If it's a current account
return withCurrent;
// else
return withSaving;
}
If you construct the deserializer yourself use
deser.setTypeResolver(MyClass::determineType);
When configuring with properties.
spring.json.value.type.method=com.mycompany.MyCass.determineType
You have to inspect the data or headers (or topic) to determine which type you want.
EDIT
Here is a complete example. In this case, I pass a type hint in the Account object, but an alternative would be to set a header on the producer side.
#SpringBootApplication
public class JacksonApplication {
public static void main(String[] args) {
SpringApplication.run(JacksonApplication.class, args);
}
#Data
public static class Employee<T extends Account> {
private String name;
private T account;
}
#Data
public static abstract class Account {
private final String type;
protected Account(String type) {
this.type = type;
}
}
#Data
public static class CurrentAccount extends Account {
private BigDecimal balance;
private BigDecimal limit;
public CurrentAccount() {
super("C");
}
}
#Data
public static class SavingAccount extends Account {
private BigDecimal balance;
public SavingAccount() {
super("S");
}
}
#KafkaListener(id = "empListener", topics = "employees")
public void listen(Employee<Account> e) {
System.out.println(e);
}
#Bean
public NewTopic topic() {
return TopicBuilder.name("employees").partitions(1).replicas(1).build();
}
#Bean
public ApplicationRunner runner(KafkaTemplate<String, Employee> template) {
return args -> {
Employee<CurrentAccount> emp1 = new Employee<>();
emp1.setName("someOneWithACurrentAccount");
CurrentAccount currentAccount = new CurrentAccount();
currentAccount.setBalance(BigDecimal.ONE);
currentAccount.setLimit(BigDecimal.TEN);
emp1.setAccount(currentAccount);
template.send("employees", emp1);
Employee<SavingAccount> emp2 = new Employee<>();
emp2.setName("someOneWithASavingAccount");
SavingAccount savingAccount = new SavingAccount();
savingAccount.setBalance(BigDecimal.ONE);
emp2.setAccount(savingAccount);
template.send("employees", emp2);
};
}
private static final JavaType withCurrent = TypeFactory.defaultInstance()
.constructParametricType(Employee.class, CurrentAccount.class);
private static final JavaType withSaving = TypeFactory.defaultInstance()
.constructParametricType(Employee.class, SavingAccount.class);
public static JavaType determineType(String topic, byte[] data, Headers headers) throws IOException {
if (JsonPath.read(new ByteArrayInputStream(data), "$.account.type").equals("C")) {
return withCurrent;
}
else {
return withSaving;
}
}
}
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.value.type.method=com.example.demo.JacksonApplication.determineType
Result
JacksonApplication.Employee(name=someOneWithACurrentAccount, account=JacksonApplication.CurrentAccount(balance=1, limit=10))
JacksonApplication.Employee(name=someOneWithASavingAccount, account=JacksonApplication.SavingAccount(balance=1))
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jackson</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
EDIT2
And here is an example that conveys the type hint in a header instead...
#SpringBootApplication
public class JacksonApplication {
public static void main(String[] args) {
SpringApplication.run(JacksonApplication.class, args);
}
#Data
public static class Employee<T extends Account> {
private String name;
private T account;
}
#Data
public static abstract class Account {
}
#Data
public static class CurrentAccount extends Account {
private BigDecimal balance;
private BigDecimal limit;
}
#Data
public static class SavingAccount extends Account {
private BigDecimal balance;
}
#KafkaListener(id = "empListener", topics = "employees")
public void listen(Employee<Account> e) {
System.out.println(e);
}
#Bean
public NewTopic topic() {
return TopicBuilder.name("employees").partitions(1).replicas(1).build();
}
#Bean
public ApplicationRunner runner(KafkaTemplate<String, Employee> template) {
return args -> {
Employee<CurrentAccount> emp1 = new Employee<>();
emp1.setName("someOneWithACurrentAccount");
CurrentAccount currentAccount = new CurrentAccount();
currentAccount.setBalance(BigDecimal.ONE);
currentAccount.setLimit(BigDecimal.TEN);
emp1.setAccount(currentAccount);
template.send("employees", emp1);
Employee<SavingAccount> emp2 = new Employee<>();
emp2.setName("someOneWithASavingAccount");
SavingAccount savingAccount = new SavingAccount();
savingAccount.setBalance(BigDecimal.ONE);
emp2.setAccount(savingAccount);
template.send("employees", emp2);
};
}
private static final JavaType withCurrent = TypeFactory.defaultInstance()
.constructParametricType(Employee.class, CurrentAccount.class);
private static final JavaType withSaving = TypeFactory.defaultInstance()
.constructParametricType(Employee.class, SavingAccount.class);
public static JavaType determineType(String topic, byte[] data, Headers headers) throws IOException {
if (headers.lastHeader("accountType").value()[0] == 'C') {
return withCurrent;
}
else {
return withSaving;
}
}
public static class MySerializer extends JsonSerializer<Employee<?>> {
#Override
public byte[] serialize(String topic, Headers headers, Employee<?> emp) {
headers.add(new RecordHeader("accountType",
new byte[] { (byte) (emp.getAccount() instanceof CurrentAccount ? 'C' : 'S')}));
return super.serialize(topic, headers, emp);
}
}
}
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.producer.value-serializer=com.example.demo2.JacksonApplication.MySerializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.value.type.method=com.example.demo2.JacksonApplication.determineType
This annotation solved my problem
#JsonTypeInfo(use = JsonTypeInfo.Id.CLASS,include = JsonTypeInfo.As.PROPERTY,property = "#class")
private T account
it binds defined class for generic to the field

Can't get xmlelements' values when it uppercase

Can't get the xml elements "CustNo" and "Name" value in the POST API using Spring Boot (It happens with the elements' names in uppercase)
The Custromer class:
#XmlAccessorType(XmlAccessType.PROPERTY)
#XmlType(name = "", propOrder = {
"custNo",
"name"
})
#XmlRootElement(name = "customer")
public class Customer {
#XmlElement(name = "CustNo")
private int custNo;
#XmlElement(name = "Name")
private String name;
#XmlElement(name = "CustNo")
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
#XmlElement(name = "Name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
the XML request Body:
<customer>
<CustNo>100</CustNo>
<Name>Alex</Name>
</customer>
Controller:
#PostMapping(path = "/save-cust-info", consumes = MediaType.APPLICATION_XML_VALUE)
public String customerInformation(#RequestBody Customer cust) {
return "Customer information saved successfully ::." + cust.getCustNo() + " " + cust.getName();
}
POM dependencies:
<properties>
<java.version>11</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
The result is: "Customer information saved successfully ::.0 null United States"
But value CustNo is 0 and Name is null. What did i wrong?
Thanks for any help in advance.
Thank you, Cezary Butler, spring ignored annotations
the solution - is to replace #XmlElement to #JacksonXmlProperty
This is how i solved it:
#JacksonXmlRootElement(localName = "customer")
public class Customer {
private String country;
#JacksonXmlProperty(localName="CustNo")
private int custNo;
#JacksonXmlProperty(localName="Name")
private String name;
public Customer() {
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>LATEST</version>
</dependency>

Configuring additional spring configuration metadata for list-nested properties

I have the following model:
#ConfigurationProperties("example.foo")
class Foo {
private String name;
private List<Bar> bars;
public static class Bar {
private String url;
private String type;
}
}
I have configured properties for top-level class like this (in additional-spring-configuration-metadata.json):
{
"properties": [
{
"name": "example.foo.name",
"type": "java.lang.String",
"description": "The name of foo"
},
{
"name": "example.foo.bars",
"type": "java.util.List<com.example.Foo.Bar>",
"description": "These are bars of foo"
}
]
}
How can I add metadata for Bar.url and Bar.type? I tried using something like example.foo.bars[].url but it didn't work.
Make sure you have getters and setters in both Foo and Bar classes. This configuration worked for me (I am using Lombok to generate getters/setters):
#ConfigurationProperties("example.foo")
#Getter
#Setter
class Foo {
private String name;
private List<Bar> bars;
#Getter
#Setter
public static class Bar {
private String url;
private String type;
}
}
Metadata generated by spring-boot-configuration-processor:
{
"properties": [
{
"name": "example.foo.name",
"type": "java.lang.String",
"sourceType": "com.demo.Foo"
},
{
"name": "example.foo.bars",
"type": "java.util.List<com.example.Foo.Bar>",
"sourceType": "com.demo.Foo"
}
]
}

How to generate json by using lombok+gson using builder() method?

I have a json like this, how can I generate it by using lombok expression + gson library? It has a mixture of array and list. Is there any readymade tool available?
{
"transactions": [
{
"transactionIds": 123456,
"test": 3000,
"amount": {
"currency": "USD",
"value": 10
}
}
]
}
Define the values like you always do and rather generating the getters / setters you would be adding the #Data attribute, Complete code below along with the dependencies
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
public class Stack32 {
public static #Data class Amount {
private String currency;
private int value;
}
public static #Data class Child {
private int transactionIds;
private int test;
private Amount amount;
}
public static #Data class Parent {
private List<Child> transactions = new ArrayList<Child>();
}
public static void main(String[] args) throws JsonProcessingException {
Amount a1 = new Amount();
a1.setCurrency("USD");
a1.setValue(10);
Child a2 = new Child();
a2.setTransactionIds(123456);
a2.setTest(3000);
a2.setAmount(a1);
Parent a3 = new Parent();
a3.getTransactions().add(a2);
ObjectMapper mapper = new ObjectMapper();
String payload = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(a3);
System.out.println(payload);
}
}
Depdendencies :
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>

Resources