amazon api 请求返回response xml解析

private void setProductWarehouse(FbaWarehouse fbaWarehouse,OrderItem orderItem, ProductsInfo product) {
        GetMatchingProductForIdRequest request=new GetMatchingProductForIdRequest();
        request.setSellerId(fbaWarehouse.getSellerId());
        request.setMarketplaceId(fbaWarehouse.getMarketPlaceId());
        request.setIdType("SellerSKU");
        List id=new ArrayList<>();
        id.add(orderItem.getSellerSKU());
        IdListType idList=new IdListType();
        idList.setId(id);
        request.setIdList(idList);
        MarketplaceWebServiceProductsClient serviceProductsClient=amazonProductSdk.createMarketplaceWebService(fbaWarehouse);
        try {
            GetMatchingProductForIdResponse response=serviceProductsClient.getMatchingProductForId(request);
            ResponseHeaderMetadata rhmd = response.getResponseHeaderMetadata();
            logger.info("setProductWarehouse respon header:"+rhmd.toString());

            logger.info("setProductWarehouse respon toXML:"+response.toXML());

            if(response.isSetGetMatchingProductForIdResult()){
                List amazonProducts=response.getGetMatchingProductForIdResult();
                if(amazonProducts.size()<1){
                    logger.info("amazonProducts size <1,SellerSKU"+orderItem.getSellerSKU());
                    return;
                }

                for (GetMatchingProductForIdResult result : amazonProducts) {
                    if("SellerSKU".equals(result.getIdType())
                            &&orderItem.getSellerSKU().equals(result.getId())
                            &&"Success".equals(result.getStatus())){
                        if(result.getProducts().getProduct().size()>1){
                            logger.info("amazon import SellerSKU:"+orderItem.getSellerSKU()+" size>1");
                            break;
                        }
                        for(Product amazonProduct : result.getProducts().getProduct()){
                            for (Object obj : amazonProduct.getAttributeSets().getAny()) {
                                Node nd =(Node)obj;
                                for(int i=0;i
                                    if("ns2:PartNumber".equals(nd.getChildNodes().item(i).getNodeName())){
                                        String manufactoryPartNmuber = StringUtils.trim(nd.getChildNodes().item(i).getTextContent());
                                        if (StringUtils.isNotBlank(manufactoryPartNmuber)) {
                                            // Pattern p = Pattern.compile("^.*#wh=[1-9]{1}[0-9]{1}$");
                                            logger.info("sku=" + orderItem.getSellerSKU() + " ;manufactoryPartNmuber=" + manufactoryPartNmuber);
                                            Pattern p = Pattern.compile("^.*#wh=(21|23|28)$", Pattern.DOTALL);
                                            if (p.matcher(manufactoryPartNmuber).matches()) {
                                                String whId = manufactoryPartNmuber.substring(manufactoryPartNmuber.length() - 2);
                                                Warehouse warehouse = product.new Warehouse();
                                                warehouse.setId(Integer.valueOf(whId));
                                                warehouse.setQty(orderItem.getQuantityOrdered());
                                                product.getWarehouses().add(warehouse);
                                                logger.info("setProductWarehouse get warehouseId:"+whId);
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

            }else{
                logger.info("GetMatchingProductForIdResponse not set result"+ ToStringBuilder.reflectionToString(response));
            }
        }catch (MarketplaceWebServiceProductsException ex) {
            // Exception properties are important for diagnostics.
            logger.info("Service Exception:");
            ResponseHeaderMetadata rhmd = ex.getResponseHeaderMetadata();
            if(rhmd != null) {
                logger.info("RequestId: "+rhmd.getRequestId());
                logger.info("Timestamp: "+rhmd.getTimestamp());
            }
            logger.info("Message: "+ex.getMessage());
            logger.info("StatusCode: "+ex.getStatusCode());
            logger.info("ErrorCode: "+ex.getErrorCode());
            logger.info("ErrorType: "+ex.getErrorType());
            throw ex;
        }
    }
@Service
public class AmazonProductSdk {
    private static final Logger logger = LoggerFactory.getLogger(AmazonProductSdk.class);
    @Autowired
    private FbaProductMatcher fbaProductMatcher;

    public MarketplaceWebServiceProductsClient createMarketplaceWebService(FbaWarehouse fbaWarehouse) {

        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();

        String endpointUrl = fbaProductMatcher.getFbaProductMatcher(fbaWarehouse.getWarehouseCode());
        config.setServiceURL(endpointUrl);
        logger.info("MarketplaceWebServiceProductsClient"+ ToStringBuilder.reflectionToString(config));

        MarketplaceWebServiceProductsClient service = new MarketplaceWebServiceProductsClient(
                fbaWarehouse.getAccessKeyId(),
                fbaWarehouse.getSecretAccessKey(),
                fbaWarehouse.getApplicationName(),
                fbaWarehouse.getApplicationVersion(),
                config);
        return service;
    }


}
@Component
public class FbaProductMatcher implements InitializingBean {

    private Map fbaProductMatchers = new HashMap();

    private final String CAEndPoint="https://mws.amazonservices.ca/Products/2011-10-01";
    private final String USEndPoint="https://mws.amazonservices.com/Products/2011-10-01";
    private final String DEEndPoint="https://mws-eu.amazonservices.com/Products/2011-10-01";
    private final String ESEndPoint="https://mws-eu.amazonservices.com/Products/2011-10-01";
    private final String FREndPoint="https://mws-eu.amazonservices.com/Products/2011-10-01";
    private final String INEndPoint="https://mws.amazonservices.in/Products/2011-10-01";
    private final String ITEndPoint="https://mws-eu.amazonservices.com/Products/2011-10-01";
    private final String UKEndPoint="https://mws-eu.amazonservices.com/Products/2011-10-01";
    private final String JPEndPoint="https://mws.amazonservices.jp/Products/2011-10-01";
    private final String CNEndPoint="https://mws.amazonservices.com.cn/Products/2011-10-01";

    @Override
    public void afterPropertiesSet() throws Exception {
        fbaProductMatchers.put(FbaWareHouseCode.CA, CAEndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.ES, ESEndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.DE, DEEndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.FR, FREndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.IT, ITEndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.UK, UKEndPoint);
        fbaProductMatchers.put(FbaWareHouseCode.US_HOME, USEndPoint);
    }

    public String getFbaProductMatcher(String fbaWareHouseCode) {
        return fbaProductMatchers.get(FbaWareHouseCode.valueOf(fbaWareHouseCode));
    }
}

package org.w3c.dom.Node;

你可能感兴趣的:(amazon api 请求返回response xml解析)