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')

此方式设置的开发者信息版本,优先级为线程,即在当前线程中生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值