Spring Boot Hibernate RestFull Service PostgreSQL -


i'm new user of spring , want develop restfull service hibernate-postgresql , spring boot. try learn documentation of spring have lot of problem deploy simple service. don't use xml file properties java class.

here different files :

persistancejpaconfig.java :

package com.spring.configuration;  import javax.sql.datasource; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.jdbc.datasource.drivermanagerdatasource; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.database; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter;  @configuration public class persistencejpaconfig {   @bean   public datasource datasource() {     drivermanagerdatasource driver = new drivermanagerdatasource();     driver.setdriverclassname("org.postgresql.driver");     driver.seturl("jdbc:postgresql://localhost:5432/test");     driver.setusername("test");     driver.setpassword("test");     return driver;   }    @bean   public localcontainerentitymanagerfactorybean entitymanagerfactory() {     hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter();     vendoradapter.setdatabase(database.postgresql);     vendoradapter.setgenerateddl(true);      localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean();     factory.setjpavendoradapter(vendoradapter);     factory.setpackagestoscan(getclass().getpackage().getname());     factory.setdatasource(datasource());      return factory;   }    @bean   @autowired   public jpatransactionmanager transactionmanager() {     jpatransactionmanager txmanager = new jpatransactionmanager();     txmanager.setentitymanagerfactory(entitymanagerfactory().getobject());      return txmanager;   } } 

i have classic model , here repository :

package com.spring.persistence.repositories;  import com.spring.persistence.model.applicationuser; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.data.jpa.repository.jparepository; import org.springframework.stereotype.repository;  @repository @qualifier(value = "applicationuserrepository") public interface applicationuserrepository extends jparepository<applicationuser,long>{  } 

a simple service :

package com.spring.persistence.service;  import com.spring.persistence.model.applicationuser; import com.spring.persistence.repositories.applicationuserrepository; import java.util.list; import javax.annotation.postconstruct; import javax.transaction.transactional; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service;  @service @transactional public class applicationuserservice {      private final applicationuserrepository applicationuserrepository;      @autowired     public applicationuserservice(applicationuserrepository applicationuserrepository) {       this.applicationuserrepository = applicationuserrepository;     }       public applicationuser createuser(string username, string type, string country)     {         applicationuser user = new applicationuser(username,type,country);         return applicationuserrepository.saveandflush(user);     }     public list<applicationuser> getalluser()     {       return applicationuserrepository.findall();     }     public applicationuser getuser(long id)     {         applicationuser user = null;         if(id != null)         {             user = applicationuserrepository.findone(id);         }         return user;     }     public boolean deleteuser(long id)     {         if(id != null)         {             try{                 applicationuserrepository.delete(id);                 return true;             }             catch(illegalargumentexception ex)             {                 ex.printstacktrace();                 return false;             }         }         else         {             system.out.println("id null");             return false;         }        } } 

and webcontroller :

@restcontroller @requestmapping(value = "/applicationuser") public class applicationusercontroller {      @autowired     private applicationuserservice applicationuserservice;      @requestmapping(value="/",method = requestmethod.get)     @responsebody     public applicationuser index()     {         return applicationuserservice.createuser("test", "test", "test");      }  } 

it's possible missing lot of things (annotation,initializer,code) i'm here learn , advices can me.

thanks answers

spring data rest

this project allow achieve goals less boilerplate code.

follow accessing jpa data rest guide demonstrates how configure spring boot + spring data rest absolutely minimal configuration.

once have basic understanding, can add more functionality meet business requirements.

detailed information provided in spring data rest documentation


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -