新增一個叫dao的package
再新增一個叫ProductRepository.java檔要選用interface
<Product,Long>的Product是Entity和Model表面上很類似,似乎都是反映資料狀態的物件。
而Long是Primary Key是指用來識別記錄的唯一性,它不可以重複及. 空值(Null)。
import com.huang.ecommerce.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product,Long> {
}
再新增一個叫ProductCategoryRepository.java檔要選用interface然後同上做JpaRepository<ProductCategory,Long> 要記得import ProductCategory再多加@RepositoryRestResource
其中@RepositoryRestResource(collectionResourceRel = "productCategory",path = "product-category")的productCategory是JSON entry而path = "product-category"
import com.huang.ecommerce.entity.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "productCategory",path = "product-category")
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Long> {
}
按SpringBootEcommerceApplication來RUN看看
結果發現一直有java11不能用的狀況
查了這篇https://stackoverflow.com/questions/54137286/error-java-invalid-target-release-11-intellij-idea/54215199
也試了用內建的jdk升級
希望會成功~因為已經是重蓋地2次了QQ
結果卡在8080連不上~QQ給我點時間來debug...
解決方法:我是windows10
就是打開cmd
然後打上netstat -ano|findstr 8080
看看是被誰listening
到工作管理員->看服務->同樣編號的停止
就出來了 http://localhost:8080/api
http://localhost:8080/api/products
先繼續建好~新增一個叫config的package
再新增一個叫MyDataRestConfig.java檔
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
public class MyDataRestConfig implements RepositoryRestConfigurer {
}
然後按右鍵Generate選implement Methods選第1個
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
public class MyDataRestConfig implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
}
}
在裡面打入"灰色"部分不是用打的
import com.huang.ecommerce.entity.Product;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;
public class MyDataRestConfig implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
HttpMethod[] theUnsupportedActions={HttpMethod.PUT,HttpMethod.POST,HttpMethod.DELETE};
config.getExposureConfiguration()
.forDomainType(Product.class)
.withItemExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)))
.withCollectionExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)));
}
在最上面加上@Configuration
import com.huang.ecommerce.entity.Product;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.http.HttpMethod;
@Configuration
public class MyDataRestConfig implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
HttpMethod[] theUnsupportedActions={HttpMethod.PUT,HttpMethod.POST,HttpMethod.DELETE};
config.getExposureConfiguration()
.forDomainType(Product.class)
.withItemExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)))
.withCollectionExposure(((metdata, httpMethods) -> httpMethods.disable(theUnsupportedActions)));
}
}
按SpringBootEcommerceApplication來RUN看看
結果卡在8080連不上~QQ給我點時間來debug...
解決方法:我是windows10
就是打開cmd
然後打上netstat -ano|findstr 8080
看看是被誰listening
到工作管理員->看服務->同樣編號的停止
用POSTMAN看http://localhost:8080/api/products
下面就要
接續第11篇的ANGULAR
DEAR ALL 我們明天見