Skip to content
Permalink
Browse files

Add Spring Boot example (#767)

  • Loading branch information...
Agraphie authored and npurushe committed Apr 14, 2018
1 parent 2ca049a commit eb8bd9e30e88a1d73ab284458d78455ab0200c3d
@@ -0,0 +1 @@
/build
@@ -0,0 +1,40 @@
buildscript {
ext {
springBootVersion = '2.0.1.RELEASE'
}
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath "net.ltgt.gradle:gradle-apt-plugin:0.6"
}
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: "net.ltgt.apt"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'io.requery.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.7

repositories {
mavenCentral()
}


dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
runtime('com.h2database:h2')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.boot:spring-boot-starter-webflux')
compile project(':requery')
compile project(':requery-jackson')
apt project(':requery-processor')
}
@@ -0,0 +1,44 @@
package io.requery.example.springboot;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.requery.Persistable;
import io.requery.cache.WeakEntityCache;
import io.requery.example.springboot.entity.Models;
import io.requery.jackson.EntityMapper;
import io.requery.sql.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.Executors;

@Configuration
public class DatabaseConfiguration {

@Bean
public ObjectMapper objectMapper(@Autowired EntityDataStore entityDataStore) {
return new EntityMapper(Models.DEFAULT, entityDataStore);
}

@Bean
public EntityDataStore<Persistable> provideDataStore() {
ConnectionProvider connectionProvider = new ConnectionProvider() {
@Override
public Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
}
};
io.requery.sql.Configuration configuration = new ConfigurationBuilder(connectionProvider, Models.DEFAULT)
.useDefaultLogging()
.setEntityCache(new WeakEntityCache())
.setWriteExecutor(Executors.newSingleThreadExecutor())
.build();

SchemaModifier tables = new SchemaModifier(configuration);
tables.createTables(TableCreationMode.DROP_CREATE);
return new EntityDataStore<>(configuration);
}
}
@@ -0,0 +1,29 @@
package io.requery.example.springboot;

import io.requery.Persistable;
import io.requery.cache.WeakEntityCache;
import io.requery.example.springboot.entity.Models;
import io.requery.meta.EntityModel;
import io.requery.sql.*;
import io.requery.sql.platform.H2;
import io.requery.sql.platform.SQLite;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.sql.CommonDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.Executors;

@SpringBootApplication
public class ExampleSpringbootApplication {

public static void main(String[] args) {
SpringApplication.run(ExampleSpringbootApplication.class, args);
}


}
@@ -0,0 +1,26 @@
package io.requery.example.springboot.controller;

import io.requery.example.springboot.entity.User;
import io.requery.example.springboot.entity.UserEntity;
import io.requery.example.springboot.repository.UserRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController("/user")
public class UserController {

@Autowired
UserRepositoryImpl userRepository;

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User register(@RequestBody UserEntity user) {
return userRepository.save(user);
}

@GetMapping
public User get(@RequestParam("id") int id) {
return userRepository.findById(id);
}
}
@@ -0,0 +1,47 @@
package io.requery.example.springboot.entity;

import io.requery.Entity;
import io.requery.Generated;
import io.requery.Key;
import io.requery.Table;

@Entity
@Table(name = "example_user")
public class User {
@Key
int id;
String firstName;
String lastName;

public User() {
}

public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}
@@ -0,0 +1,13 @@
package io.requery.example.springboot.repository;

import io.requery.example.springboot.entity.User;

public interface UserRepository {
User findById(int id);

User save(User user);

void delete(User user);

void delete(int id);
}
@@ -0,0 +1,33 @@
package io.requery.example.springboot.repository;

import io.requery.example.springboot.entity.User;
import io.requery.example.springboot.entity.UserEntity;
import io.requery.sql.EntityDataStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
EntityDataStore dataStore;

@Override
public User findById(int id) {
return (User) dataStore.findByKey(User.class, id);
}

@Override
public User save(User user) {
return (User) dataStore.upsert(user);
}

@Override
public void delete(User user) {
dataStore.delete(user);
}

@Override
public void delete(int id) {
dataStore.delete(UserEntity.class).where(UserEntity.ID.eq(id));
}
}
@@ -0,0 +1,74 @@
package io.requery.example.springboot;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.requery.example.springboot.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ExampleSpringbootApplicationTest {
private MockMvc mockMvc;

@Autowired
private WebApplicationContext wac;
private User testUser;
private ObjectMapper objectMapper;

@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
objectMapper = new ObjectMapper();
testUser = new User("John", "Smith");
testUser.setId(1);
}

@Test
public void createUser() throws Exception {
String userJson = objectMapper.writeValueAsString(testUser);
postUserAndExpectSame(userJson);
}

@Test
public void findUserById() throws Exception {
String userJson = objectMapper.writeValueAsString(testUser);
mockMvc.perform(
post("/user")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(userJson));

mockMvc.perform(get("/user" + 1)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(userJson));
}

@Test
public void updateUser() throws Exception {
String userJson = objectMapper.writeValueAsString(testUser);
postUserAndExpectSame(userJson);
testUser.setFirstName("Henry");
userJson = objectMapper.writeValueAsString(testUser);
postUserAndExpectSame(userJson);
}

private void postUserAndExpectSame(String userJson) throws Exception {
mockMvc.perform(
post("/user")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(userJson)
).andExpect(status().isCreated()).andExpect(content().json(userJson));
}
}
@@ -8,3 +8,4 @@ include ':requery-android:example-kotlin'
include ':requery-jackson'
include ':requery-kotlin'
include ':requery-test:kotlin-test'
include ':requery:example-springboot'

0 comments on commit eb8bd9e

Please sign in to comment.
You can’t perform that action at this time.