java开发亚马逊mws_GitHub - iotwlw/Amazon-MWS-SDK: 基于亚马逊MWS Java SDK 的封装
amazon-mws-java-sdk
亞馬遜MWS服務的Java-SDK封裝
安裝
在pom.xml中添加依賴
top.guyi.amazon
Amazon-MWS-SDK
1.0.0.1
依賴放在nexus私服中,并沒有上傳到中央倉庫,所以請在pom.xml中加入倉庫配置
guyi-maple-nexus
http://nexus.guyi-maple.space/nexus/content/groups/public/
本項目基于Spring-Boot,依賴添加后會自行進行相關配置
你需要做的只是在application.properties中給出亞馬遜MWS的開發者信息
如下:
application.properties
guyi.amazon.mws.accessKey =
guyi.amazon.mws.secretKey =
guyi.amazon.mws.server = https://mws.amazonservices.jp #mws端點
guyi.amazon.mws.sellerId =
guyi.amazon.mws.marketplaceId =
guyi.amazon.mws.charset = Shift_JIS #編碼
關于亞馬遜MWS端點,請參見 亞馬遜MWS服務端點列表
編碼設置用于數據上傳接口中,具體編碼信息,請參見 SubmitFeed Content-Type
開發者信息
如果你需要同時操作多個亞馬遜商城,且使用不同的開發者信息,請實現接口 AmazonConfigProvider
然后將你的實現類放置到Spring容器中
AmazonConfigProvider接口中的version方法返回當前開發者信息的版本名稱
String version()
在使用時可使用此版本名稱進行切換
@Resource
AmazonConfigFactory factory
factory.currentVersion(version) //設置當前線程下使用的開發者信息
factory.current() //獲取當前線程下的開發者信息
factory.globalVersion(version) //設置全局開發者信息
factory.globalVersion() //獲取全局開發者信息
開發者信息版本的優先級為:
Thread(當前線程) -> global(全局) -> default(application.properties中的配置)
接口調用
新增接口調用流程
SuperAmazonClient Client接口
泛型
泛型 C 對應亞馬遜提供的Java客戶端中的config類,如
MarketplaceWebServiceProductsConfig,MarketplaceWebServiceConfig等
泛型 E 對應亞馬遜提供的Java客戶端中的Client類,如
MarketplaceWebServiceProductsClient,MarketplaceWebServiceClient等
新增Client類
需要自己新增Client類時,請實現接口 SuperAmazonClient,如下示例實現類
ProductsClient.groovy
package top.guyi.amazon.mws.client.impl
import com.amazonservices.mws.products.MarketplaceWebServiceProductsClient
import com.amazonservices.mws.products.MarketplaceWebServiceProductsConfig
import top.guyi.amazon.mws.client.SuperAmazonClient
import top.guyi.amazon.mws.conf.AmazonConfigProvider
/**
* Created by 古逸 on 2017-05-26.
*/
class ProductsClient implements SuperAmazonClient {
@Override
MarketplaceWebServiceProductsClient create(AmazonConfigProvider provider) {
MarketplaceWebServiceProductsConfig config = this.newConfig(provider)
return this.createByConfig(provider,config)
}
@Override
MarketplaceWebServiceProductsClient createByConfig(AmazonConfigProvider provider, MarketplaceWebServiceProductsConfig config) {
MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(provider.accessKey(),provider.secretKey(),config)
return client
}
@Override
MarketplaceWebServiceProductsConfig newConfig(AmazonConfigProvider provider) {
MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig()
config.serviceURL = provider.server()
return config
}
}
SuperRequestPackers Request基類
泛型
泛型 T 表示實現類本身,如實現類名為GetMatchingProductForIdPackers,那么泛型T 就為 GetMatchingProductForIdPackers
泛型 R 對應亞馬遜Java客戶端中的請求,如
GetMatchingProductForIdRequest,GetLowestOfferListingsForASINRequest,GetLowestOfferListingsForSKURequest等
泛型 P 對應Response類,及響應的處理,有關于Respose類的介紹,請參見下方解釋
新增Request類
要新增Request類,請實現基類SuperRequestPackers
實現類中的 method 方法返回當前請求調用的亞馬遜哪一個接口,首字母小寫
String method()
亞馬遜接口列表,請參見 亞馬遜MWS服務文檔
當請求中有必須的參數時,建議添加must方法,如果沒有,則不需要
示例代碼如下
package top.guyi.amazon.mws.request.impl.products
import com.amazonservices.mws.products.model.GetMatchingProductForIdRequest
import com.amazonservices.mws.products.model.IdListType
import top.guyi.amazon.mws.client.SuperAmazonClient
import top.guyi.amazon.mws.client.impl.ProductsClient
import top.guyi.amazon.mws.request.SuperRequestPackers
import top.guyi.amazon.mws.response.impl.products.GetMatchingProductForIdHandler
/**
* Created by 古逸 on 2017-06-01.
*/
class GetMatchingProductForIdPackers extends SuperRequestPackers{
private GetMatchingProductForIdRequest request
@Override
Class extends SuperAmazonClient> clientClass() {
return ProductsClient.class
}
@Override
String method() {
return 'getMatchingProductForId'
}
GetMatchingProductForIdPackers must(String idType,List idList){
this.amazonRequest().idType = idType
IdListType list = new IdListType()
list.id = idList
this.amazonRequest().idList = list
return this
}
@Override
GetMatchingProductForIdRequest amazonRequest() {
if(request == null){
request = new GetMatchingProductForIdRequest()
request.sellerId = this.config().current().sellerId()
request.marketplaceId = this.config().current().marketplaceId()
}
return request
}
}
SuperResponseHandler
響應基類
泛型
泛型 P 對應亞馬遜Java客戶端中的響應,如
GetMatchingProductForIdResponse,GetLowestOfferListingsForASINResponse,GetLowestPricedOffersForASINResponse等
泛型 R 表示Request類,關于Request類請參見上方的解釋
新增Response類
新增Response類,需要實現基類 SuperResponseHandler
實現類中的 pattern 方法表示處理返回的XML數據,請返回EntityData實體類的數組
EntityData實體類中包含數據處理的正則表達式與Lambda表達式
如果返回 null ,表示不進行處理
示例代碼如下
package top.guyi.amazon.mws.response.impl.report
import com.amazonservices.mws.products.model.GetMatchingProductRequest
import com.amazonservices.mws.products.model.GetMatchingProductResponse
import top.guyi.amazon.mws.response.SuperResponseHandler
import top.guyi.amazon.mws.response.entity.product.Product
/**
* Created by 古逸 on 2017-05-26.
*/
class GetMatchingProductHandler extends SuperResponseHandler{
List childs = new LinkedList()
Product parent
@Override
String getXML(GetMatchingProductResponse response) {
return response.toXML()
}
@Override
List pattern() {
String xml = this.response().toXML()
if(xml.contains('ns2:VariationChild')){
return [
EntityData.create(/(.*?)(.*?)/,
{line->
Product product = new Product()
product.marketplaceId = line[1]
product.asin = line[2]
childs.add(product)
})
]
}else if(xml.contains('VariationParent')){
return [
EntityData.create(/(.*?)(.*?)/,
{line->
parent = new Product()
parent.marketplaceId = line[1]
parent.asin = line[2]
})
]
}
}
}
注意
新增的Client、Request、Response實現類,如果要使其生效,都需要放入到Spring的容器中
接口調用方式
接口調用時,每一次調用請新建Request對象,此對象沒有做單例處理,請盡量將其作為局部變量使用
如:
GetMatchingProductPackers packers = new GetMatchingProductPackers()
GetMatchingProductHandler response = packers.must('B00S7W8TUQ').invok()
println(response.childs)
println(response.parent)
println(response.getXML())
println(response.response())
Request類中集成了自定義參數的配置方式,如下示例
GetMatchingProductPackers packers = new GetMatchingProductPackers()
packers.customize {request->
ASINListType list = new ASINListType()
list.ASIN = ['B00S7W8TUQ']
request.ASINList = list
}
GetMatchingProductHandler response = packers.invok()
println(response.childs)
println(response.parent)
println(response.getXML())
println(response.response())
Request類中集成了自定義開發者信息的配置方式,此配置方式只在當前的Request對象中生效,如下示例
GetMatchingProductPackers packers = new GetMatchingProductPackers()
packers.customizeAmazonConfig {config->
config.serviceURL = 'https://mws.amazonservices.jp'
config.connectionTimeout = 3000
}
GetMatchingProductHandler response = packers.must('B00S7W8TUQ').invok()
println(response.childs)
println(response.parent)
println(response.getXML())
println(response.response())
Request類支持Builder設計模式,如下示例
println(new GetMatchingProductPackers().must('B00S7W8TUQ').invok().parent())
println(new GetMatchingProductPackers()
.customize({request->
ASINListType list = new ASINListType()
list.ASIN = ['B00S7W8TUQ']
request.ASINList = list
}).invok().parent())
在Request中更改開發者信息的版本
當你需要在Request中更改開發者信息版本是,可調用 version 方法
GetMatchingProductPackers packers = new GetMatchingProductPackers()
packers.version('Japan')
此方式設置的開發者信息版本,優先級為線程,即在當前線程中生效
總結
以上是生活随笔為你收集整理的java开发亚马逊mws_GitHub - iotwlw/Amazon-MWS-SDK: 基于亚马逊MWS Java SDK 的封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab错误原因,matlab常见错
- 下一篇: 服务启动不了,显示 config 异常的