config cors header

This commit is contained in:
Chantha
2020-05-17 13:35:32 +07:00
parent 5e35060d53
commit ba9e100a18
11 changed files with 69 additions and 34 deletions

View File

@@ -0,0 +1,13 @@
package com.chantha.jdbc.config
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@Configuration
class CORSConfig :WebMvcConfigurer{
override fun addCorsMappings(registry: CorsRegistry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3001")
}
}

View File

@@ -9,11 +9,15 @@ import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
public class ProductController @Autowired constructor(private val productService: ProductService) {
public class ProductController @Autowired constructor(private val productService: ProductService,private val productRepo: ProductRepo) {
@GetMapping("/product")
@GetMapping("/api/product/order")
fun product():List<ProductWithOrderDetail>{
return productService.fetchAllProducts()
}
@GetMapping("/api/product/getproduct")
fun getAllProduct():List<Product>{
return productRepo.readAllRecord()
}
}

View File

@@ -8,6 +8,7 @@ import java.util.List;
@Entity
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "productId")
@JsonPropertyOrder({"productId"})
public class Product implements Serializable {
@Id
@@ -17,6 +18,16 @@ public class Product implements Serializable {
private String productName;
private double price;
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
private String img;
public Product() {
}
@@ -53,11 +64,12 @@ public class Product implements Serializable {
this.orderDetails = orderDetails;
}
public Product(Long productId, String productName, double price, List<OrderDetail> orderDetails) {
public Product(Long productId, String productName, double price, List<OrderDetail> orderDetails,String img) {
this.productId=productId;
this.productName=productName;
this.price=price;
this.orderDetails=orderDetails;
this.img=img;
}
// @JsonManagedReference

View File

@@ -10,4 +10,8 @@ interface ProductRepo : CrudRepository<Product,Long>{
@Query("SELECT * FROM product INNER JOIN order_detail ON (product.product_id=order_detail.product_id)"
, nativeQuery = true)
override fun findAll(): MutableIterable<Product>
@Query("SELECT * FROM product",nativeQuery = true)
fun readAllRecord():List<Product>
}

View File

@@ -16,6 +16,8 @@ class ProductServiceImpl @Autowired constructor(private val productRepo: Product
return productRepo.findAll().map { toView(it) }
}
private fun toView(data: Product): ProductWithOrderDetail {
val view = ProductWithOrderDetail()
view.productId = data.productId