Articles
Software
Gallery
Other works
Contacts

#The most minimalistic JPA|REST example with spring.html



        {
          // --- AnimalRepository.java ---
            /* By using the Farm package here,
             * we make the contents of
             * FarmApplication.java visible
             */
            package com.example.Farm;

            import org.springframework.data.jpa.repository.JpaRepository;

            public interface AnimalRepository extends JpaRepository {}

          // --- FarmApplication.java ---
            /* By using the Farm package here,
             * we make the contents of
             * AnimalRepository.java visible
             */
            package com.example.Farm;

            import java.util.List;
            import jakarta.persistence.*;
            import org.springframework.boot.*;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.context.annotation.*;
            import org.springframework.web.bind.annotation.*;

            /* Signals spring to perform component scanning.
             */
            @SpringBootApplication
            public
            class FarmApplication {
                /* We mark Animal as and @Entity to signal to JTA that
                 *  we wish to abstarct this class as a database table.
                 */
                @Entity
                /* We declare Animal nested only for simplicity.
                 */
                public static
                class Animal {
                    /* We mark id as the primary key for our table.
                     *  It could be further used by our program for querying, etc.  
                     */
                    @Id
                    @GeneratedValue(strategy = GenerationType.IDENTITY)
                    Long id;

                    // - Public
                    // will be visible by (API) queries
                    public String name;

                    // - Private
                    // will NOT be visible by (API) queries
                    private int weight;

                    // - Private \w Getter
                    // will be visible by (API) queries
                    private int age;
                    public int getAge(){
                        return this.age;
                    }

                    /* Constructor to be used only by US later on.
                     */
                    Animal(String name, int age, int weight) {
                        this.name = name;
                        this.age = age;
                        this.weight = weight;
                    }

                    /* Default constuctor required by @Entity
                     *  since we have another constuctor,
                     *  we have to specify it explicitly.
                     */
                    Animal() { ; }
                }
                
                /* Marking cmdlr as @Bean makes it managed by spring.
                 * In this case it means that it will be called on start up.
                 */
                @Bean
                public
                /* Spring  expects  a
                 *  CommandLineRunner       Spring will pass the othetwise
                 *  instance for start       private "global" repository
                 *  up configurations        instance for us
                 *        |                           |
                 *        V                           V             */
                CommandLineRunner cmdlr(AnimalRepository repository) {
                    return (args) -> {
                        repository.save(new Animal("Bob", 3, 10));
                    };
                }

                /* Marking this class as a @RestController makes spring scan it
                 *  for @*Mapping-s.
                 *  Otherwise they would be ignored.
                 */
                @RestController
                static
                class Controller {
                    /* Internal copy of the global repository instance.
                     */
                    private final
                    AnimalRepository repository;

                    Controller(AnimalRepository repository) {
                        this.repository = repository;
                    }
                    
                    /* Associate the calling of this function
                     *  with GET requests arriving to :URL:/hello
                     */
                    @GetMapping("/hello")
                    /* Making the return type List will
                     *  result in a JSON of N auto serialized
                     *  Animal instances
                     */
                    List say_hello(){
                        return repository.findAll();  // findAll is inherited by AnimalRepository
                    }
                }

                /* Finally, launch or spring application at once.
                 * As we insstantiate SpringApplication and there by the spring eco-system,
                 *  we trigger its commandline, Inversion of Control, Database and web service
                 *  features in the process.
                 */
                public static
                void main(String[] args) {
                    SpringApplication.run(FarmApplication.class, args);
                }
            }
        }