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>
Related
When I use Spring Boot 2.7.2 everything works ok. After upgrade to version 3.0.0-SNAPSHOT, I have
My pom.xml
<?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>3.0.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring_jwt</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>spring-boot-security-jwt</name>
<description>spring_jwt</description>
<properties>
<java.version>18</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>1.6.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-functions</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-metadata</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-chart-themes</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-annotation-processors</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-castor</artifactId>
<version>6.20.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>net.sf.jasperreports</groupId>-->
<!-- <artifactId>liberation-fonts</artifactId>-->
<!-- <version>1.0</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/com.mpobjects.jasperreports.font/jasperreports-fonts-liberation -->
<dependency>
<groupId>com.mpobjects.jasperreports.font</groupId>
<artifactId>jasperreports-fonts-liberation</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-chart-customizers</artifactId>
<version>6.20.0</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-custom-visualization</artifactId>
<version>6.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-report</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-web</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-base</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-viewer</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-samples</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-webviewer</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-lib</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>stimulsoft-reports-webdesigner</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.stimulsoft</groupId>-->
<!-- <artifactId>reports</artifactId>-->
<!-- <version>2022.3.3</version>-->
<!-- <type>pom</type>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven-central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
Entity
package com.example.BLModel;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.Table;
import java.time.OffsetDateTime;
#Entity
#Table(name = "account")
public class Account {
#EmbeddedId
private AccountId id;
#MapsId("tenantId")
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "tenant_id", nullable = false)
private Tenant tenant;
#Column(name = "account_number", nullable = false, length = 32)
private String accountNumber;
#Column(name = "account_name", nullable = false, length = 128)
private String accountName;
#Column(name = "account_name_english", length = 128)
private String accountNameEnglish;
#Column(name = "account_name_chinese", length = 128)
private String accountNameChinese;
#Column(name = "account_name_korean", length = 128)
private String accountNameKorean;
#Column(name = "description", length = 512)
private String description;
#Column(name = "parent_id")
private Short parentId;
#Column(name = "internal_code_id", length = 128)
private String internalCodeId;
#Column(name = "grade")
private Short grade;
#Column(name = "is_parent", nullable = false)
private Boolean isParent = false;
#Column(name = "account_category_kind", nullable = false)
private Short accountCategoryKind;
#Column(name = "is_postable_in_foreign_currency", nullable = false)
private Boolean isPostableInForeignCurrency = false;
#Column(name = "detail_by_account_object", nullable = false)
private Boolean detailByAccountObject = false;
#Column(name = "account_object_type")
private Short accountObjectType;
#Column(name = "detail_by_bank_account", nullable = false)
private Boolean detailByBankAccount = false;
#Column(name = "detail_by_job", nullable = false)
private Boolean detailByJob = false;
#Column(name = "detail_by_job_kind")
private Short detailByJobKind;
#Column(name = "detail_by_project_work", nullable = false)
private Boolean detailByProjectWork = false;
#Column(name = "detail_by_project_work_kind")
private Short detailByProjectWorkKind;
#Column(name = "detail_by_order", nullable = false)
private Boolean detailByOrder = false;
#Column(name = "detail_by_order_kind")
private Short detailByOrderKind;
#Column(name = "detail_by_contract", nullable = false)
private Boolean detailByContract = false;
#Column(name = "detail_by_contract_kind")
private Short detailByContractKind;
#Column(name = "detail_by_expense_item", nullable = false)
private Boolean detailByExpenseItem = false;
#Column(name = "detail_by_expense_item_kind")
private Short detailByExpenseItemKind;
#Column(name = "detail_by_department", nullable = false)
private Boolean detailByDepartment = false;
#Column(name = "detail_by_department_kind")
private Short detailByDepartmentKind;
#Column(name = "detail_by_list_item", nullable = false)
private Boolean detailByListItem = false;
#Column(name = "detail_by_list_item_kind")
private Short detailByListItemKind;
#Column(name = "active_status", nullable = false)
private Boolean activeStatus = false;
#Column(name = "created")
private OffsetDateTime created;
#Column(name = "created_by", length = 64)
private String createdBy;
#Column(name = "modified")
private OffsetDateTime modified;
#Column(name = "modified_by", length = 64)
private String modifiedBy;
#Column(name = "sort_internal_code_id", length = 128)
private String sortInternalCodeId;
#Column(name = "detail_by_pu_contract", nullable = false)
private Boolean detailByPuContract = false;
#Column(name = "detail_by_pu_contract_kind")
private Short detailByPuContractKind;
public AccountId getId() {
return id;
}
public void setId(AccountId id) {
this.id = id;
}
public Tenant getTenant() {
return tenant;
}
public void setTenant(Tenant tenant) {
this.tenant = tenant;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getAccountNameEnglish() {
return accountNameEnglish;
}
public void setAccountNameEnglish(String accountNameEnglish) {
this.accountNameEnglish = accountNameEnglish;
}
public String getAccountNameChinese() {
return accountNameChinese;
}
public void setAccountNameChinese(String accountNameChinese) {
this.accountNameChinese = accountNameChinese;
}
public String getAccountNameKorean() {
return accountNameKorean;
}
public void setAccountNameKorean(String accountNameKorean) {
this.accountNameKorean = accountNameKorean;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Short getParentId() {
return parentId;
}
public void setParentId(Short parentId) {
this.parentId = parentId;
}
public String getInternalCodeId() {
return internalCodeId;
}
public void setInternalCodeId(String internalCodeId) {
this.internalCodeId = internalCodeId;
}
public Short getGrade() {
return grade;
}
public void setGrade(Short grade) {
this.grade = grade;
}
public Boolean getIsParent() {
return isParent;
}
public void setIsParent(Boolean isParent) {
this.isParent = isParent;
}
public Short getAccountCategoryKind() {
return accountCategoryKind;
}
public void setAccountCategoryKind(Short accountCategoryKind) {
this.accountCategoryKind = accountCategoryKind;
}
public Boolean getIsPostableInForeignCurrency() {
return isPostableInForeignCurrency;
}
public void setIsPostableInForeignCurrency(Boolean isPostableInForeignCurrency) {
this.isPostableInForeignCurrency = isPostableInForeignCurrency;
}
public Boolean getDetailByAccountObject() {
return detailByAccountObject;
}
public void setDetailByAccountObject(Boolean detailByAccountObject) {
this.detailByAccountObject = detailByAccountObject;
}
public Short getAccountObjectType() {
return accountObjectType;
}
public void setAccountObjectType(Short accountObjectType) {
this.accountObjectType = accountObjectType;
}
public Boolean getDetailByBankAccount() {
return detailByBankAccount;
}
public void setDetailByBankAccount(Boolean detailByBankAccount) {
this.detailByBankAccount = detailByBankAccount;
}
public Boolean getDetailByJob() {
return detailByJob;
}
public void setDetailByJob(Boolean detailByJob) {
this.detailByJob = detailByJob;
}
public Short getDetailByJobKind() {
return detailByJobKind;
}
public void setDetailByJobKind(Short detailByJobKind) {
this.detailByJobKind = detailByJobKind;
}
public Boolean getDetailByProjectWork() {
return detailByProjectWork;
}
public void setDetailByProjectWork(Boolean detailByProjectWork) {
this.detailByProjectWork = detailByProjectWork;
}
public Short getDetailByProjectWorkKind() {
return detailByProjectWorkKind;
}
public void setDetailByProjectWorkKind(Short detailByProjectWorkKind) {
this.detailByProjectWorkKind = detailByProjectWorkKind;
}
public Boolean getDetailByOrder() {
return detailByOrder;
}
public void setDetailByOrder(Boolean detailByOrder) {
this.detailByOrder = detailByOrder;
}
public Short getDetailByOrderKind() {
return detailByOrderKind;
}
public void setDetailByOrderKind(Short detailByOrderKind) {
this.detailByOrderKind = detailByOrderKind;
}
public Boolean getDetailByContract() {
return detailByContract;
}
public void setDetailByContract(Boolean detailByContract) {
this.detailByContract = detailByContract;
}
public Short getDetailByContractKind() {
return detailByContractKind;
}
public void setDetailByContractKind(Short detailByContractKind) {
this.detailByContractKind = detailByContractKind;
}
public Boolean getDetailByExpenseItem() {
return detailByExpenseItem;
}
public void setDetailByExpenseItem(Boolean detailByExpenseItem) {
this.detailByExpenseItem = detailByExpenseItem;
}
public Short getDetailByExpenseItemKind() {
return detailByExpenseItemKind;
}
public void setDetailByExpenseItemKind(Short detailByExpenseItemKind) {
this.detailByExpenseItemKind = detailByExpenseItemKind;
}
public Boolean getDetailByDepartment() {
return detailByDepartment;
}
public void setDetailByDepartment(Boolean detailByDepartment) {
this.detailByDepartment = detailByDepartment;
}
public Short getDetailByDepartmentKind() {
return detailByDepartmentKind;
}
public void setDetailByDepartmentKind(Short detailByDepartmentKind) {
this.detailByDepartmentKind = detailByDepartmentKind;
}
public Boolean getDetailByListItem() {
return detailByListItem;
}
public void setDetailByListItem(Boolean detailByListItem) {
this.detailByListItem = detailByListItem;
}
public Short getDetailByListItemKind() {
return detailByListItemKind;
}
public void setDetailByListItemKind(Short detailByListItemKind) {
this.detailByListItemKind = detailByListItemKind;
}
public Boolean getActiveStatus() {
return activeStatus;
}
public void setActiveStatus(Boolean activeStatus) {
this.activeStatus = activeStatus;
}
public OffsetDateTime getCreated() {
return created;
}
public void setCreated(OffsetDateTime created) {
this.created = created;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public OffsetDateTime getModified() {
return modified;
}
public void setModified(OffsetDateTime modified) {
this.modified = modified;
}
public String getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
public String getSortInternalCodeId() {
return sortInternalCodeId;
}
public void setSortInternalCodeId(String sortInternalCodeId) {
this.sortInternalCodeId = sortInternalCodeId;
}
public Boolean getDetailByPuContract() {
return detailByPuContract;
}
public void setDetailByPuContract(Boolean detailByPuContract) {
this.detailByPuContract = detailByPuContract;
}
public Short getDetailByPuContractKind() {
return detailByPuContractKind;
}
public void setDetailByPuContractKind(Short detailByPuContractKind) {
this.detailByPuContractKind = detailByPuContractKind;
}
}
error
java: package javax.persistence does not exist
How to fix?
There is no need to add hibernate-entitymanager artifact as dependency in pom.xml.
spring-boot-starter-data-jpa artifact is enough. You shoud just consider the following points.
Replace Java EE 8 With Jakarta EE 9 APIs:
Spring Boot 3.0 will be the first version of Spring Boot that makes use of Jakarta EE 9 APIs (jakarta.) instead of EE 8 (javax.). This entails that we will have to look for EE 8 imports with javax.* and replace them with jakarta.*. Typical EE 8 packages used in Spring Boot microservices include the following:
javax.persistence.*
javax.validation.*
javax.servlet.*
javax.annotation.*
javax.transaction.*
etc.
Be aware that packages such as javax.sql.* and javax.crypto.* come from Java 17 JDK, not from EE 8, so they are safe to stay.
References:
Notes on Spring-Boot-3 Upgrade
Preparing for Spring-Boot-3-0
Change "JPA API from Java EE" --> "JPA API from Jakarta EE", and add
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>6.0.0.Alpha7</version>
<type>pom</type>
</dependency>
Just delete the jakarta dependency. Since it has its full content in the ultimate version of Intellij Idea. In your case, this one:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
And add these dependencies:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
</dependency>
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
Hi I have a normal working application which is inserting / deleting / updating correctly. However I am trying to implement receiving a json string from ajax and inserting it in a database (PostgreSQL 10.12)
The first 2 to 5 records get inserted correctly however then the application starts updating records instead of inserting and the primary keys get out of sequence.
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dashboard</groupId>
<artifactId>sp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring </name>
<description>Spring boot </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.13.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
#SuppressWarnings("hiding")
#MappedSuperclass
public abstract class AbstractModel<Long extends Serializable> implements Serializable {
private static final long serialVersionUID = -6323358535657100144L;
#Id
#GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractModel<?> other = (AbstractModel<?>) obj;
if (id == null) {
return other.id == null;
} else return id.equals(other.id);
}
}
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Entity;
#Entity
#Data
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
#ToString
public class PurchaseOrderProducts extends AbstractModel<Long> {
/**
*
*/
private static final long serialVersionUID = -5927643103467927664L;
#Column(nullable = false)
private Integer productcode;
#Column(nullable = false)
private String manufacturer;
#Column(nullable = false)
private String model;
#Column(nullable = false)
private String supplier;
#Column(nullable = false)
private String supplierproductcode;
#Column(nullable = false)
private Integer orderno;
public Integer getProductcode() {
return productcode;
}
public void setProductcode(Integer productcode) {
this.productcode = productcode;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getSupplierproductcode() {
return supplierproductcode;
}
public void setSupplierproductcode(String supplierproductcode) {
this.supplierproductcode = supplierproductcode;
}
public Integer getOrderno() {
return orderno;
}
public void setOrderno(Integer orderno) {
this.orderno = orderno;
}
#Override
public String toString() {
return "PurchaseOrderProducts [productcode=" + productcode + ", manufacturer=" + manufacturer + ", model="
+ model + ", supplier=" + supplier + ", supplierproductcode=" + supplierproductcode + ", orderno="
+ orderno + "]";
}
}
#Service
public class PurchaseOrdersProductsService extends AbstractService<PurchaseOrderProducts, Long> {
#Autowired
private PurchaseOrdersProductsRepository purchaseOrdersProductsRepository;
#Override
protected JpaRepository<PurchaseOrderProducts, Long> getRepository() {
return purchaseOrdersProductsRepository;
}
public List<PurchaseOrderProducts> getAllPurchaseOrdersProducts() {
return getRepository().findAll();
}
#Transactional
public boolean checkIfExists(Integer productcode, Integer orderno) {
boolean val = false;
Integer test = purchaseOrdersProductsRepository.checkIfExists(productcode,orderno);
if(test == 0) {
val = false;
} else {
val = true;
}
return val;
}
public void saveAll(List<PurchaseOrderProducts> purchaseOrderProducts) {
purchaseOrdersProductsRepository.saveAll(purchaseOrderProducts);
}
}
#Repository
public interface PurchaseOrdersProductsRepository extends JpaRepository<PurchaseOrderProducts, Long> {
#Query(value = "SELECT count(productcode) FROM purchase_order_products m WHERE m.productcode = ?1 AND m.orderno=?2" , nativeQuery=true)
Integer checkIfExists(long productcode, long orderno);
}
#PostMapping(value = "/submitpoproducts", consumes = "application/json", produces = "application/json")
#ResponseBody
public List<String> savePurchaseOrderProductList(#RequestBody PurchaseOrderProducts[] purchaseOrderProducts) {
List<String> response = new ArrayList<String>();
for (int i = 0; i < purchaseOrderProducts.length; i++) {
// if (purchaseOrderProducts[i].getProductcode() != null) {
// if
// (purchaseOrdersProductsService.checkIfExists(purchaseOrderProducts[i].getProductcode(),
// purchaseOrderProducts[i].getOrderno()) == false) {
purchaseOrdersProductsService.save(purchaseOrderProducts[i]);
System.out.println(purchaseOrderProducts[i].toString());
// }
// }
// response.add("Saved product: " + purchaseOrderProducts[i]);
}
return response;
}
PurchaseOrderProducts [productcode=10000004, manufacturer=FS, model=CL 20cl, supplier=Fs, supplierproductcode=FAR001, orderno=10000003]
id |
----+
1 |
2 |
3 |
4 |
id |
----+--------------
1 |
3 |
4 |
2 |
5 |
It is hard to test your issue as you didn't provide the JSON. As a best pratice, you should check the client is not sending PurchaseOrderProducts with id, this may be your issue.
For anyone else that might come across this. I removed the json key value pair being sent having the key id and it worked. (with javascript )
delete row['id'];
<?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 http://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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>2018Spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>2018Spring</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</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-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>
package com.example.spring.model;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection="courses")
public class Course {
#Id
private String _id;
private String name;
private String code;
private String pass;
private String lecture;
private List<ObjectId> subjects;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getLecture() {
return lecture;
}
public void setLecture(String lecture) {
this.lecture = lecture;
}
public List<ObjectId> getSubjects() {
return subjects;
}
public void setSubjects(List<ObjectId> subjects) {
this.subjects = subjects;
}
}
https://start.spring.io/
You can create a project here and it'll take care of all the configurations to start.
pom.xml likes this.in spring-boot-starter-web,it already has hibernate-validator dependency.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
ClassRequest
#Data
public class ClassRequest {
String glade;
Integer number;
List<StudentRequest> students;
}
StudentRequest
import lombok.Data;
import javax.validation.constraints.NotNull;
#Data
public class StudentRequest {
String name;
Integer age;
#NotNull(message="id not blank")
String identity;
}
controller
#RestController
public class StudentController {
#PostMapping("/class/info/create")
public #ResponseBody Response createClassInfo(#RequestBody #Valid
ClassRequest classRequest) {
System.out.println(classRequest.getNumber());
return Response.ok;
}
}
Do I miss something?the validation not working.
I am learner. can some one help me?
it is Nesting.it can validate classRequest. it can not validate studentRequest.just add #valid.
#Data
public class ClassRequest {
String glade;
Integer number;
#valid
List<StudentRequest> students;
}