Вот мои файлы:
AttributeTransformer.java
package com.ebay.app.compliance;
import com.bazaarvoice.jolt.Chainr;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List; // Import List
import java.util.Map;
public class AttributeTransformer {
private final Chainr chainr;
private final Map rules;
private final ObjectMapper objectMapper = new ObjectMapper();
/**
* Constructor loads the JOLT specification and compliance rules from classpath resources.
* In a real microservice (e.g., using Spring Boot), this would be done once at startup.
*/
public AttributeTransformer() throws IOException {
// Load the JOLT transformation spec from the classpath
InputStream specStream = this.getClass().getResourceAsStream("/spec.json");
if (specStream == null) {
throw new IOException("Could not find spec.json in classpath.");
}
// Use Jackson ObjectMapper to parse the spec directly into a List.
List specJson = objectMapper.readValue(specStream, new TypeReference() {});
this.chainr = Chainr.fromSpec(specJson);
// Load the business rules from the classpath
InputStream rulesStream = this.getClass().getResourceAsStream("/rules.json");
if (rulesStream == null) {
throw new IOException("Could not find rules.json in classpath.");
}
// Use Jackson to parse the rules JSON into a Java Map
this.rules = objectMapper.readValue(rulesStream, new TypeReference() {});
}
/**
* Transforms an input payload into compliance attributes.
*
* @param inputJson The input from the classification service (e.g., {"product_class": "Knife", "product_subclass": "Throwing Knife"})
* @return A JSON string of the transformed attributes.
* @throws IOException if JSON processing fails.
*/
public String transform(String inputJson) throws IOException {
// Parse the input JSON string into a Java Map
Map inputMap = objectMapper.readValue(inputJson, new TypeReference() {});
// The JOLT transform method can take a "context" map, which allows the spec
// to reference external data. We'll provide our rules here.
Map context = Map.of("rules", this.rules);
// Perform the transformation
Object transformedOutput = chainr.transform(inputMap, context);
// Convert the final Java Object back to a pretty-printed JSON string
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(transformedOutput);
}
/**
* Main method to act as a test harness for our transformer.
*/
public static void main(String[] args) {
try {
// 1. Initialize the transformer (loads specs and rules)
AttributeTransformer transformer = new AttributeTransformer();
// 2. Define the input payload from the upstream service
String inputPayload = "{\"product_class\": \"Knife\", \"product_subclass\": \"Throwing Knife\"}";
System.out.println("---
System.out.println("Input Payload: " + inputPayload);
System.out.println("----------------------------------------");
// 3. Run the transformation
String finalOutput = transformer.transform(inputPayload);
// 4. Print the result
System.out.println("Final Output:");
System.out.println(finalOutput);
System.out.println("---
} catch (IOException e) {
System.err.println("Failed to initialize or run transformer.");
e.printStackTrace();
}
}
}
< /code>
rules.json
{
"Knife": {
"Cutlery Knife": {
"is_bladed": true,
"is_prohibited_ca": false,
"requires_age_verification": false,
"compliance_policy_id": "CP-102"
},
"Throwing Knife": {
"is_bladed": true,
"is_prohibited_ca": true,
"requires_age_verification": true,
"compliance_policy_id": "CP-451"
}
},
"Lighter": {
"Default": {
"is_bladed": false,
"is_hazmat_ground": true,
"is_hazmat_air": true,
"compliance_policy_id": "CP-210"
}
}
}
< /code>
And my spec.json
[
{
"operation": "shift",
"spec": {
"rules": {
"@1,product_class": {
"*": {
"@2,&": {
"@4,product_subclass": {
"*": {
"@2,&": "attributes"
}
}
}
}
}
},
"*": "&"
}
}
]
< /code>
My expected output is
---
Input Payload: {"product_class": "Knife", "product_subclass": "Throwing Knife"}
----------------------------------------
Final Output:
{
"product_class" : "Knife",
"product_subclass" : "Throwing Knife",
"attributes" : {
"is_bladed" : true,
"is_prohibited_ca" : true,
"requires_age_verification" : true,
"compliance_policy_id" : "CP-451"
}
}
---
< /code>
actual output
---
Input Payload: {"product_class": "Knife", "product_subclass": "Throwing Knife"}
----------------------------------------
Final Output:
{
"product_class" : "Knife",
"product_subclass" : "Throwing Knife"
}
---
Подробнее здесь: https://stackoverflow.com/questions/797 ... azaarvoice