Пример простого клиент-серверного RESTful решения на основе Spring Boot. Серверная часть build.gradle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
buildscript { ext { springBootVersion = '1.2.5.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") classpath("io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'io.spring.dependency-management' jar { baseName = 'restboot' version = '0.0.1-SNAPSHOT' } sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test") compile("com.fasterxml.jackson.core:jackson-databind:2.6.0") compile('mysql:mysql-connector-java:5.1.36') compile('com.google.guava:guava:18.0') } eclipse { classpath { containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' } } task wrapper(type: Wrapper) { gradleVersion = '2.3' } |
Структура проекта: App:
1 2 3 4 5 6 |
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } } |
Config:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Configuration public class Config { @Bean public DataSource dataSource(){ DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/springmvc"); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("admin"); return dataSource; } } |
Contact:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package demo.dom; import javax.persistence.*; @Entity(name = "rest_contacts") public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) Integer id; @Column String name; @Column String number; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } } |
Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package demo.controllers; import com.google.common.collect.Lists; import demo.dom.Contact; import demo.repositories.ContactRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/contact") public class Controller { @Autowired ContactRepository contactRepository; @RequestMapping(value = "/list", method = RequestMethod.GET) public Object[] getContacts(){ return Lists.newArrayList(contactRepository.findAll()).toArray(); } @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Contact getContact(@PathVariable Integer id){ return contactRepository.findOne(id); } @RequestMapping(value = "/" , method = RequestMethod.POST) public void saveContact(@RequestBody Contact contact){ contactRepository.save(contact); } @RequestMapping(value = "/", method = RequestMethod.DELETE) public void deleteContact(@RequestBody Contact contact){ contactRepository.delete(contact); } } |
Если не указать параметр method аннотации RequestMapping, то аннотированный метод будет принимать все виды запросов сразу ContactRepository