spring-boot
जेपीए के साथ स्प्रिंग-बूट माइक्रोसर्विस
खोज…
आवेदन कक्षा
package com.mcf7.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataMicroServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataMicroServiceApplication.class, args);
}
}
पुस्तक मॉडल
package com.mcf7.spring.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
@lombok.Getter
@lombok.Setter
@lombok.EqualsAndHashCode(of = "isbn")
@lombok.ToString(exclude="id")
@Entity
public class Book implements Serializable {
public Book() {}
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private long id;
@NotNull
@Size(min = 1)
private String isbn;
@NotNull
@Size(min = 1)
private String title;
@NotNull
@Size(min = 1)
private String author;
@NotNull
@Size(min = 1)
private String description;
}
बस कुछ बातों के बाद से यहाँ एक नोट चल रहा है, मैं उन्हें जल्दी असली तोड़ना चाहता था।
@ लेम्बोक के साथ सभी एनोटेशन हमारी कक्षा की बॉयलर प्लेट में से कुछ उत्पन्न कर रहे हैं
@lombok.Getter //Creates getter methods for our variables
@lombok.Setter //Creates setter methods four our variables
@lombok.EqualsAndHashCode(of = "isbn") //Creates Equals and Hashcode methods based off of the isbn variable
@lombok.ToString(exclude="id") //Creates a toString method based off of every variable except id
हमने इस ऑब्जेक्ट में मान्यता भी ली है
@NotNull //This specifies that when validation is called this element shouldn't be null
@Size(min = 1) //This specifies that when validation is called this String shouldn't be smaller than 1
बुक रिपोजिटरी
package com.mcf7.spring.domain;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
}
बेसिक स्प्रिंग रिपॉजिटरी पैटर्न, इसके अलावा हमने अतिरिक्त सुविधाओं के लिए पेजिंग और सॉर्टिंग रिपॉजिटरी को सक्षम किया .... जैसे पेजिंग और सॉर्टिंग :)
सत्यापन सक्षम करना
package com.mcf7.spring.domain;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class BeforeCreateBookValidator implements Validator{
public boolean supports(Class<?> clazz) {
return Book.class.equals(clazz);
}
public void validate(Object target, Errors errors) {
errors.reject("rejected");
}
}
कुछ परीक्षण डेटा लोड हो रहे हैं
package com.mcf7.spring.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class DatabaseLoader implements CommandLineRunner {
private final BookRepository repository;
@Autowired
public DatabaseLoader(BookRepository repository) {
this.repository = repository;
}
public void run(String... Strings) throws Exception {
Book book1 = new Book();
book1.setIsbn("6515616168418510");
book1.setTitle("SuperAwesomeTitle");
book1.setAuthor("MCF7");
book1.setDescription("This Book is super epic!");
repository.save(book1);
}
}
बस कुछ परीक्षण डेटा लोड करना आदर्श रूप से यह केवल एक विकास प्रोफ़ाइल के तहत जोड़ा जाना चाहिए।
मान्यवर जोड़ना
package com.mcf7.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@Configuration
public class RestValidationConfiguration extends RepositoryRestConfigurerAdapter {
@Bean
@Primary
/**
* Create a validator to use in bean validation - primary to be able to autowire without qualifier
*/
Validator validator() {
return new LocalValidatorFactoryBean();
}
@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
Validator validator = validator();
//bean validation always before save and create
validatingListener.addValidator("beforeCreate", validator);
validatingListener.addValidator("beforeSave", validator);
}
}
ग्रेड फ़ाइल बनाएँ
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE'
}
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'idea'
apply plugin: 'java'
dependencyManagement {
imports {
mavenBom 'io.spring.platform:platform-bom:2.0.5.RELEASE'
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.springframework.boot:spring-boot-starter-data-rest'
compile 'org.springframework.data:spring-data-rest-hal-browser'
compile 'org.projectlombok:lombok:1.16.6'
compile 'org.springframework.boot:spring-boot-starter-validation'
compile 'org.springframework.boot:spring-boot-actuator'
runtime 'com.h2database:h2'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
}
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow