如何在java中使用camel来封送具有固定长度数据格式的重复组的JSON?

dzjeubhm  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(232)

我需要处理一个JSON -它有重复的组,我需要将其转换为固定长度的记录

{
    "blockType" : "BL-H ",
    "blockTypeLength" : "00000031",
    "blockId" : "S62951156229900",
    "totalNoOfTX" : "001",
    "msgblockType" : "TX-S ",
    "messageLength" : "00000728",
    "noa":[
        {
            "title":"Behin",
            "artist":"LIMP ",
            "itunes_link":"http:behind"
        },
        {
            "title":"Alone",
            "artist":"ALYSS",
            "itunes_link":"http:clk.doubler.com"
        }
    ]

}

应转换为BehinLIMPclk.doubler.com
请求中可以有一个或多个这样的组
我试过的-
第一个
我已经使用了相应的模型来保存json和@FixedLengthRecord的值,需要帮助来理解和解决这个问题。

public class ZvkkRequest {
@DataField(pos = 1, length=5, align = "L", paddingChar=' ')
private String blockType;
@DataField(pos = 2, length=8, align = "R", paddingChar='0')
private int blockTypeLength;
@DataField(pos = 3, length=15, align = "L", paddingChar=' ')
private String blockId;
@DataField(pos = 4, length=3, align = "R", paddingChar='0')
private int totalNoOfTX;
@DataField(pos = 5, length=5, align = "L", paddingChar=' ')
private String msgblockType;
@DataField(pos = 6, length=8, align = "R", paddingChar='0')
private int messageLength;

@OneToMany(mappedTo = "classpath.DAOFix")
private List<DAOFix> repeatingGrp;
}

class DAOFix {

@DataField(pos = 7, length=5, align = "R", paddingChar=' ')
private String title;
@DataField(pos = 8, length=5, align = "R", paddingChar=' ')
private String artist;
@DataField(pos = 9, length=5, align = "R", paddingChar=' ')
private String itunes_link;

}

eiee3dmh

eiee3dmh1#

经过测试,@OneToMany似乎不能像BindyCsvDataFormat那样与BindyFixedLengthDataFormat一起工作,这意味着您可能会处理json并创建多个对象示例,然后您可以使用Bindy将这些示例编组为固定长度的记录。
下面是我用来测试这一点的代码,以及一种将单个ZvkkRequest示例转换为ZvkkRequestFixedLenght示例列表的方法(direct:bindyFixedLenghtProcessed),然后可以将这些示例列表编组为BindyFixedLengthDataFormat

public class ExampleTests extends CamelTestSupport {

    @Test
    public void bindyCSVTest(){

        // Test bindy @OneToMany with BindyCsvDataFormat 
        template.sendBody("direct:bindyCSV", testJson);
    }

    @Test
    public void bindyFixedLenghtTest(){

        // Test bindy @OneToMany with BindyFixedLengthDataFormat
        template.sendBody("direct:bindyFixedLenght", testJson);
    }

    @Test
    public void bindyFixedLenghtProcessedTest(){

        // Test workaraound 
        template.sendBody("direct:bindyFixedLenghtProcessed", testJson);
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {

        return new RouteBuilder(){

            @Override
            public void configure() throws Exception {

                // Doesn't seem to work with OneToMany
                DataFormat bindyCSV = new BindyCsvDataFormat(ZvkkRequest.class);

                from("direct:bindyCSV")
                    .unmarshal().json(JsonLibrary.Jackson, ZvkkRequest.class)
                    .marshal(bindyCSV)
                    .log("${body}");

                DataFormat bindyFixedLenght = new BindyFixedLengthDataFormat(ZvkkRequest.class);

                from("direct:bindyFixedLenght")
                    .unmarshal().json(JsonLibrary.Jackson, ZvkkRequest.class)
                    .marshal(bindyFixedLenght)
                    .log("${body}");

                DataFormat bindyFixedLenght2 = new BindyFixedLengthDataFormat(ZvkkRequestFixedLenght.class);

                from("direct:bindyFixedLenghtProcessed")
                    .unmarshal().json(JsonLibrary.Jackson, ZvkkRequest.class)
                    .process(new Processor(){

                        @Override
                        public void process(Exchange exchange) throws Exception {

                            ZvkkRequest body = exchange.getMessage()
                                .getBody(ZvkkRequest.class);

                            List<ZvkkRequestFixedLenght> requests = new ArrayList<>();

                            for (DAOFix it : body.noa) {
                                ZvkkRequestFixedLenght request = new ZvkkRequestFixedLenght();
                                request.blockType = body.blockType;
                                request.blockTypeLength = body.blockTypeLength;
                                request.blockId = body.blockId;
                                request.totalNoOfTX = body.totalNoOfTX;
                                request.msgblockType = body.msgblockType;
                                request.messageLength = body.messageLength;
                                request.artist = it.artist;
                                request.title = it.title;
                                request.itunesLink = it.itunesLink;
                                requests.add(request);
                            }
                            exchange.getMessage().setBody(requests);
                        }

                    })
                    .marshal(bindyFixedLenght2)
                    .log("${body}");

            }
        };
    }

    String testJson = "{" +
    "    \"blockType\" : \"BL-H \"," +
    "    \"blockTypeLength\" : \"00000031\"," +
    "    \"blockId\" : \"S62951156229900\"," +
    "    \"totalNoOfTX\" : \"001\"," +
    "    \"msgblockType\" : \"TX-S \"," +
    "    \"messageLength\" : \"00000728\"," +
    "    \"noa\":[" +
    "        {" +
    "            \"title\":\"Behin\"," +
    "            \"artist\":\"LIMP \"," +
    "            \"itunes_link\":\"http:behind\"" +
    "        }," +
    "        {" +
    "            \"title\":\"Alone\"," +
    "            \"artist\":\"ALYSS\"," +
    "            \"itunes_link\":\"http:clk.doubler.com\"" +
    "        }" +
    "    ]" +
    "}";
}

绑定固定长度已处理测试记录以下内容:

BL-H 00000031S62951156229900001TX-S 00000728BehinLIMP                    http:behind
BL-H 00000031S62951156229900001TX-S 00000728AloneALYSS          http:clk.doubler.com

如果您希望将这些条目放在单行上,您可以尝试分别编组ZvkkRequestDAOFix,并使用字符串连接将解组的DAOFix条目组合到解组的ZvkkRequest中。

Zvkk请求.java

@FixedLengthRecord
@CsvRecord(separator=",")
public class ZvkkRequest {

    @DataField(pos = 1, length = 5, align = "L", paddingChar = ' ')
    public String blockType;

    @DataField(pos = 2, length = 8, align = "R", paddingChar = '0')
    public int blockTypeLength;

    @DataField(pos = 3, length = 15, align = "L", paddingChar = ' ')
    public String blockId;

    @DataField(pos = 4, length = 3, align = "R", paddingChar = '0')
    public int totalNoOfTX;

    @DataField(pos = 5, length = 5, align = "L", paddingChar = ' ')
    public String msgblockType;

    @DataField(pos = 6, length = 8, align = "R", paddingChar = '0')
    public int messageLength;

    @OneToMany
    public List<DAOFix> noa;
}

动态数据库修复程序.java

public class DAOFix {

    @DataField(pos = 7, length=5, align = "R", paddingChar=' ')
    public String title;

    @DataField(pos = 8, length=5, align = "R", paddingChar=' ')
    public String artist;

    @JsonProperty("itunes_link")
    @DataField(pos = 9, length=30, align = "R", paddingChar=' ')
    public String itunesLink;
}

Zvkk请求固定长度.java

@CsvRecord(separator=",")
@FixedLengthRecord
public class ZvkkRequestFixedLenght {

    @DataField(pos = 1, length = 5, align = "L", paddingChar = ' ')
    public String blockType;

    @DataField(pos = 2, length = 8, align = "R", paddingChar = '0')
    public int blockTypeLength;

    @DataField(pos = 3, length = 15, align = "L", paddingChar = ' ')
    public String blockId;

    @DataField(pos = 4, length = 3, align = "R", paddingChar = '0')
    public int totalNoOfTX;

    @DataField(pos = 5, length = 5, align = "L", paddingChar = ' ')
    public String msgblockType;

    @DataField(pos = 6, length = 8, align = "R", paddingChar = '0')
    public int messageLength;

    @DataField(pos = 7, length=5, align = "R", paddingChar=' ')
    public String title;

    @DataField(pos = 8, length=5, align = "R", paddingChar=' ')
    public String artist;

    @DataField(pos = 9, length=30, align = "R", paddingChar=' ')
    public String itunesLink;
}

相关问题