<properties> <spring.version>4.3.5.RELEASE</spring.version> <spring.data.version>1.11.1.RELEASE</spring.data.version> <h2.version>1.4.193</h2.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spring.data.version}</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.1.0.Final</version> </dependency> <!-- Jackson JSON Mapper --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency> </dependencies>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.0</version> </dependency>
/** * */ package com.doj.restapi.web.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** * @author Dinesh.Rajput * */ @Configuration @EnableWebMvc @ComponentScan("com.doj.restapi.web.controller") public class RestApplicationConfig{ }@EnableWebMvc- It used for enabling spring mvc behavior i.e. it is responsible for registering Spring MVC required Bean Post Processors in the application as like Jackson2JsonMessageConverter etc.
/** * */ package com.doj.restapi.config; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; /** * @author Dinesh.Rajput * */ @Configuration @ComponentScan("com.doj.restapi.service") @EnableJpaRepositories("com.doj.restapi.repository") public class InfrastructureConfig { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build(); } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter bean = new HibernateJpaVendorAdapter(); bean.setDatabase(Database.H2); bean.setGenerateDdl(true); return bean; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); bean.setDataSource(dataSource); bean.setJpaVendorAdapter(jpaVendorAdapter); bean.setPackagesToScan("com.doj.restapi.bean"); return bean; } @Bean public JpaTransactionManager transactionManager(EntityManagerFactory emf) { return new JpaTransactionManager(emf); } }@EnableJpaRepositories-It used to create the spring bean related to spring data jpa in the application i.e. those classes which implement spring data repository interfaces.
/** * */ package com.doj.restapi.web; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import com.doj.restapi.config.InfrastructureConfig; import com.doj.restapi.web.config.RestApplicationConfig; /** * @author Dinesh.Rajput * */ public class RestApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { InfrastructureConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] { RestApplicationConfig.class }; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } }
/** * */ package com.doj.restapi.web.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.doj.restapi.bean.Account; import com.doj.restapi.service.IAccountService; /** * @author Dinesh.Rajput * */ @RestController public class AccountController { @Autowired IAccountService accountService; @GetMapping("/") public String home (){ return "Spring REST Dinesh on Java!!!"; } @GetMapping("/accounts") public List<Account> all (){ return accountService.list(); } @PostMapping("/account") public Account create (@RequestBody Account account){ return accountService.create(account); } @GetMapping("/account/{accountId}") public Account get (@PathVariable Long accountId){ return accountService.get(accountId); } @PutMapping("/account/{accountId}") public Account update (@RequestBody Account account, @PathVariable Long accountId){ return accountService.update(account, accountId); } @DeleteMapping("/account/{accountId}") public void delete (@PathVariable Long accountId){ accountService.delete(accountId); } }
@RestController = @Controller + @ResponseBody
@Controller public class AccountController { ..... ..... @RequestMapping("/account/{accountId}") public @ResponseBody Account get (@PathVariable Long accountId){ return accountService.get(accountId); } ..... ..... }
@GetMapping = @RequestMapping + Http GET method
/** * */ package com.doj.restapi.bean; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * @author Dinesh.Rajput * */ @Entity @Table public class Account implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue public Long accountId; public String name; public String city; public Double balance; public Account() { super(); } public Account(String name, String city, Double balance) { super(); this.name = name; this.city = city; this.balance = balance; } public Long getAccountId() { return accountId; } public void setAccountId(Long accountId) { this.accountId = accountId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Double getBalance() { return balance; } public void setBalance(Double balance) { this.balance = balance; } @Override public String toString() { return "Account [accountId=" + accountId + ", name=" + name + ", city=" + city + ", balance=" + balance + "]"; } }
/** * */ package com.doj.restapi.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.doj.restapi.bean.Account; import com.doj.restapi.repository.AccountRepository; /** * @author Dinesh.Rajput * */ @Service public class AccountService implements IAccountService{ @Autowired AccountRepository accountRepository; @Override public Account create(Account account) { return accountRepository.save(account); } @Override public Account get(Long accountId) { return accountRepository.findOne(accountId); } @Override public Account update(Account account, Long accountId) { return accountRepository.save(account); } @Override public void delete(Long accountId) { accountRepository.delete(accountId); } @Override public List<Account> list() { return (List<Account>) accountRepository.findAll(); } }
/** * */ package com.doj.restapi.repository; import org.springframework.data.repository.CrudRepository; import com.doj.restapi.bean.Account; /** * @author Dinesh.Rajput * */ public interface AccountRepository extends CrudRepository<Account, Long> { }
Labels: REST, Spring REST, Spring4, WebService