spring mvc hibernate-validator not working - validation

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

Related

Optimistic locking Test in spring data JPA

I'm trying to test the optimistic locking mechanism in spring data jpa by loading a certain entity twice using findBy function, then updating the first one and asserting that when the second one is updated, it will throw an OptimisticLockingFailureException.
But the problem is that no exception is thrown and the second update is done successfully.
After investigation i found that findBy function hits the database only the first time and caches the returned entity. and when i call it again it returns cached entity. which means that both loaded entities are equal. so the first update reflects in both entities making the second entity does not have the stale data.
so, how do i force loading the second entity from the data base in the second findBy function call ?
Here is my code:-
Test class
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
#DataJpaTest
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class PersistenceTests {
#Autowired
private ProductRepository repository;
private ProductEntity savedEntity;
#DynamicPropertySource
static void databaseProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", () -> "jdbc:mysql://localhost:3306/code_snippet");
registry.add("spring.datasource.username", () -> "root");
registry.add("spring.datasource.password", () -> "System");
registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");
}
#BeforeEach
void setupDb() {
repository.deleteAll();
ProductEntity entity = new ProductEntity(1, "n", 1);
savedEntity = repository.save(entity);
assertEqualsProduct(entity, savedEntity);
}
#Test
void optimisticLockError() {
// Store the saved entity in two separate entity objects
ProductEntity entity1 = repository.findById(savedEntity.getId()).get();
ProductEntity entity2 = repository.findById(savedEntity.getId()).get();
// Update the entity using the first entity object
entity1.setName("n1");
repository.save(entity1);
// Update the entity using the second entity object.
// This should fail since the second entity now holds an old version number,
// i.e. an Optimistic Lock Error
assertThrows(OptimisticLockingFailureException.class, () -> {
entity2.setName("n2");
repository.save(entity2);
});
// Get the updated entity from the database and verify its new sate
ProductEntity updatedEntity = repository.findById(savedEntity.getId()).get();
assertEquals(1, (int) updatedEntity.getVersion());
assertEquals("n1", updatedEntity.getName());
}
private void assertEqualsProduct(ProductEntity expectedEntity, ProductEntity actualEntity) {
assertEquals(expectedEntity.getId(), actualEntity.getId());
assertEquals(expectedEntity.getVersion(), actualEntity.getVersion());
assertEquals(expectedEntity.getProductId(), actualEntity.getProductId());
assertEquals(expectedEntity.getName(), actualEntity.getName());
assertEquals(expectedEntity.getWeight(), actualEntity.getWeight());
}
}
Entity
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
#Entity
#Table(name = "product")
public class ProductEntity {
#Id
#GeneratedValue
private Integer id;
#Version
private Integer version;
private int productId;
private String name;
private int weight;
public ProductEntity() {
}
public ProductEntity(int productId, String name, int weight) {
this.productId = productId;
this.name = name;
this.weight = weight;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Repository
import java.util.Optional;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface ProductRepository extends PagingAndSortingRepository<ProductEntity, Integer> {
Optional<ProductEntity> findByProductId(int productId);
}
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>2.7.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.javaworld.codesnippet</groupId>
<artifactId>writing-persistence-tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>writing-persistence-tests</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
Main Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class WritingPersistenceTestsApplication {
public static void main(String[] args) {
SpringApplication.run(WritingPersistenceTestsApplication.class, args);
}
}
The problem is that your test method is by default transactional. You can disable the transactional for this method by adding:
#Test
#Transactional(value = Transactional.TxType.NEVER)
Than you get in the second save ObjectOptimisticLockingFailureException

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

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>

Springboot Jpa or hibernate updating records instead of inserting

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'];

what are the steps to be required to start with spring boot project

<?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.

Resources