Usage
Installation
To install the Spring REST Framework, include the following dependencies in your project:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>io.github.nikanique</groupId>
<artifactId>spring-rest-framework</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
Getting Started
To start using the library, follow these steps:
Add the necessary dependencies to your project:
Add the required dependencies into your project following the installation section.
Declare your models and repositories:
For example, declare a Student model.
import jakarta.persistence.Entity; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import lombok.Data; @Entity @Data public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; private String major; }
Create Repository for you model:
import com.example.demo.model.Student; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; @Repository public interface StudentRepository extends JpaRepository<Student, Long>, JpaSpecificationExecutor<Kid> { }
Configure your API endpoints and serializers DTO:
declare a DTO representing your model’s field in web API.
import io.github.nikanique.springrestframework.annotation.Expose; import io.github.nikanique.springrestframework.annotation.ReadOnly; import io.github.nikanique.springrestframework.dto.Dto; import lombok.Data; @Data public class StudentDto extends Dto{ @Expose(source = "name") private String fullName; private Integer age; private String major; @ReadOnly private Long id; }
Create your Controller by extending QueryController which will generate List and Retrieve endpoint for you.
@RequestMapping("/student") @RestController @Tag(name = "Student") public class StudentController extends QueryController<Kid, Long, StudentRepository> { public StudentController(StudentRepository repository) { super(repository); } @Override protected Class<?> getDTO() { return StudentDto.class; } }
Run your application, and enjoy your APIs.
You can see your API at http://app-server:port/swagger-ui.html