1. SpringWebApplication.java
package com.raj.springweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringwebApplication.class, args);
}
}
2. Simple HelloWorld Rest service Example.
package com.raj.springweb.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestHelloWorldController {
@RequestMapping (value="/helloapi/helloworld", method=RequestMethod.GET)
public String helloworld () {
return new String("Hello World");
}
}
3. Product Service example, which needs below set of files to be created as well.
package com.raj.springweb.controllers;
import java.util.List;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.raj.springweb.model.Product;
import com.raj.springweb.repos.ProductJpaRepository;
@RestController
public class RestProductController {
@Autowired
ProductJpaRepository productRepo;
@RequestMapping (value="/productapi/allproducts", method=RequestMethod.GET)
public List findAllProducts () {
return productRepo.findAll();
}
@RequestMapping (value="/productapi/product/byId/{id}", method=RequestMethod.GET)
public Product findProductbyId (@PathVariable("id") long id) {
return productRepo.findById(id).get();
}
@RequestMapping (value="/productapi/createproduct", method=RequestMethod.POST)
public Product createProduct (@RequestBody Product product) {
return productRepo.save(product);
}
@RequestMapping (value="/productapi/updateproduct", method=RequestMethod.PUT)
public Product updateProduct (@RequestBody Product product) {
return productRepo.save(product);
}
@RequestMapping (value="/productapi/deleteproduct/byId/{id}", method=RequestMethod.DELETE)
public void deleteProduct (@PathVariable long id) {
productRepo.deleteById(id);
}
}
4. Entity class Product with auto-generated primarykey (ID)
package com.raj.springweb.model;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
long id;
String name;
String description;
BigDecimal price;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
5. Repository Interface
package com.raj.springweb.repos;
import org.springframework.data.jpa.repository.JpaRepository;
import com.raj.springweb.model.Product;
public interface ProductJpaRepository extends JpaRepository {
}
6. Test class with Rest Client
package com.raj.springweb;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import com.raj.springweb.model.Product;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringwebApplicationTests {
@Test
public void testGetProduct() {
RestTemplate restTemplate = new RestTemplate();
Product product = restTemplate.getForObject("http://localhost:8080/service/productapi/product/byId/4", Product.class);
assertNotNull(product);
assertEquals("prod4", product.getName());
}
@Test
public void testPostProduct() {
RestTemplate restTemplate = new RestTemplate();
Product product = new Product();
product.setName("prod5");
product.setDescription("Product Five");
product.setPrice(BigDecimal.valueOf(12.50));
Product newProd = restTemplate.postForObject("http://localhost:8080/service/productapi/createproduct", product, Product.class);
assertNotNull(newProd);
assertEquals("prod5", newProd.getName());
}
}
7. application.xml
server.servlet.context-path=/service/
spring.datasource.url=jdbc:mysql://localhost:3306/springboottestdb
spring.datasource.username=root
spring.datasource.password=password
8. POM.xml
package com.raj.springweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringwebApplication.class, args);
}
}
2. Simple HelloWorld Rest service Example.
package com.raj.springweb.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestHelloWorldController {
@RequestMapping (value="/helloapi/helloworld", method=RequestMethod.GET)
public String helloworld () {
return new String("Hello World");
}
}
3. Product Service example, which needs below set of files to be created as well.
package com.raj.springweb.controllers;
import java.util.List;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.raj.springweb.model.Product;
import com.raj.springweb.repos.ProductJpaRepository;
@RestController
public class RestProductController {
@Autowired
ProductJpaRepository productRepo;
@RequestMapping (value="/productapi/allproducts", method=RequestMethod.GET)
public List
return productRepo.findAll();
}
@RequestMapping (value="/productapi/product/byId/{id}", method=RequestMethod.GET)
public Product findProductbyId (@PathVariable("id") long id) {
return productRepo.findById(id).get();
}
@RequestMapping (value="/productapi/createproduct", method=RequestMethod.POST)
public Product createProduct (@RequestBody Product product) {
return productRepo.save(product);
}
@RequestMapping (value="/productapi/updateproduct", method=RequestMethod.PUT)
public Product updateProduct (@RequestBody Product product) {
return productRepo.save(product);
}
@RequestMapping (value="/productapi/deleteproduct/byId/{id}", method=RequestMethod.DELETE)
public void deleteProduct (@PathVariable long id) {
productRepo.deleteById(id);
}
}
4. Entity class Product with auto-generated primarykey (ID)
package com.raj.springweb.model;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
long id;
String name;
String description;
BigDecimal price;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
5. Repository Interface
package com.raj.springweb.repos;
import org.springframework.data.jpa.repository.JpaRepository;
import com.raj.springweb.model.Product;
public interface ProductJpaRepository extends JpaRepository
}
6. Test class with Rest Client
package com.raj.springweb;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import com.raj.springweb.model.Product;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringwebApplicationTests {
@Test
public void testGetProduct() {
RestTemplate restTemplate = new RestTemplate();
Product product = restTemplate.getForObject("http://localhost:8080/service/productapi/product/byId/4", Product.class);
assertNotNull(product);
assertEquals("prod4", product.getName());
}
@Test
public void testPostProduct() {
RestTemplate restTemplate = new RestTemplate();
Product product = new Product();
product.setName("prod5");
product.setDescription("Product Five");
product.setPrice(BigDecimal.valueOf(12.50));
Product newProd = restTemplate.postForObject("http://localhost:8080/service/productapi/createproduct", product, Product.class);
assertNotNull(newProd);
assertEquals("prod5", newProd.getName());
}
}
7. application.xml
server.servlet.context-path=/service/
spring.datasource.url=jdbc:mysql://localhost:3306/springboottestdb
spring.datasource.username=root
spring.datasource.password=password
8. POM.xml
No comments:
Post a Comment