CouchRest is a plugin for Spring. It enables you to publish resources very fast via REST.
This project is in its early days, so there are still a lot of features to implement.
It requires at minimum Java 11.
The artifact is currently hosted on JCenter.
Gradle users:
repositories {
jcenter()
}
...
dependencies {
implementation 'com.hedgehogs-mind:spring-couch-rest'
}
Maven users:
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
...
<dependency>
<groupId>com.hedgehogs-mind</groupId>
<artifactId>spring-couch-rest</artifactId>
<version>0.0.1-ALPHA</version>
<type>pom</type>
</dependency>
In this section, we will show you, how you can enable CouchRest in your Spring (Boot) Application. After this step, CouchRest runs but will still throw an error, because we need to add a configuration. This is done in the next step!
Take your Spring Boot Application class and open it. Add the annotation @EnableCouchRest to the class.
...
import com.hedgehogsmind.springcouchrest.annotations.EnableCouchRest;
...
@SpringBootApplication
@EnableCouchRest
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
You can also configure the required beans manually. You need to configure the following beans:
CouchRestCoreCouchRestHandlerMappingCouchRestHandlerAdapter
@EnableCouchRest does nothing else than importing these classes into the Spring Application (scan).
After enabling CouchRest, you probably instantly started your application. In case I'm right, you experienced the following error:
Caused by: com.hedgehogsmind.springcouchrest.beans.exceptions.NoConfigurationFoundException: No CouchRestConfiguration found.
at com.hedgehogsmind.springcouchrest.beans.CouchRestCore.fetchCouchRestConfiguration(CouchRestCore.java:87) ~[main/:na]
at com.hedgehogsmind.springcouchrest.beans.CouchRestCore.setup(CouchRestCore.java:64) ~[main/:na]
...
You need to provide a CouchRestConfiguration Bean! For convenience, you can extend the CouchRestConfigurationAdapter.
For our first demo, we will disable all security restrictions. This is not recommended for normal usage! Please revert this later. Also read the security documentation!
Here is an example:
@Component
public class MyCouchRestConfiguration
extends CouchRestConfigurationAdapter {
@Override
public String getBaseSecurityRule() {
return "permitAll()";
}
@Override
public String getDefaultEndpointSecurityRule() {
return "permitAll()";
}
}
For more on that checkout the configuration docs as well as the security docs.
Just add the annotation @CouchRest to an entity like this:
@Entity
@CouchRest
public class Note {
@Id
@GeneratedValue
public int id;
@Column
public String title;
@Column
@Lob
public String content;
}
You can now perform GET /api/note and you will get all note instances!
You can also publish a resource via a repository by just adding the annotation @CouchRest again:
@Repository
@CouchRest
public interface AddressRepository extends CrudRepository<Address, Long> {
}
@Entity
public class Address {
@Id
@GeneratedValue
public long id;
@Column
public String street;
@Column
public String nr;
@Column
public String zip;
@Column
public String town;
}
Go ahead and read the following documentation pages, to better understand, what CouchRest is capable of:
