最近在看<<netty指南第二版>>,看到了netty结合http+xml协议栈开发,需要用到jibx,本人首次使用jibx,一时间犯了难.网上看了很多帖子,大都是下载好jar包,然后在cmd窗口中运行Java -jar命令 .由于公司内部屏蔽了许多外网导致,jibx的jar包下载不了.后来多次尝试.利用maven和ant成功实现了jibx的初次使用,特此记录.
1,本地创建maven工程,在pom中添加如下依赖
<!-- https://mvnrepository.com/artifact/org.jibx/jibx-run --> <dependency> <groupId>org.jibx</groupId> <artifactId>jibx-run</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jibx/jibx-extras --> <dependency> <groupId>org.jibx</groupId> <artifactId>jibx-extras</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jibx/jibx-bind --> <dependency> <groupId>org.jibx</groupId> <artifactId>jibx-bind</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jibx/jibx-tools --> <dependency> <groupId>org.jibx</groupId> <artifactId>jibx-tools</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.jibx/jibx-schema --> <dependency> <groupId>org.jibx</groupId> <artifactId>jibx-schema</artifactId> <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.bcel/bcel --> <dependency> <groupId>org.apache.bcel</groupId> <artifactId>bcel</artifactId> <version>6.0</version> </dependency>
待maven依赖全部下载好后在本地maven仓库中查看
jibx相关的jar:
bcel相关:
2,然后就到了比较难的地方,由于本人也是首次使用ant,所以本着摸着石头过河的心态,找了一个ant的实例改了改,在项目目录下创建build.xml,其中(maven本地仓库的路径需要改成自己的路径,实体类的全类名需要改成自己的)
<?xml version="1.0" encoding="utf-8"?> <project default="main" basedir="."> <path id="classpath"> <dirset dir="${basedir}/target/classes" /> <!--由于不需要编译单元测试代码,就注掉了下面的内容--> <!--<dirset dir="${basedir}/target/test-classes" />--> <!--下面目录为本地maven仓库的jibx和bcel的jar包的绝对路径--> <fileset dir="D:/develop/apache-maven/repository-pub/org/jibx/jibx-bind/1.3.1/" includes="*.jar" /> <fileset dir="D:/develop/apache-maven/repository-pub/org/jibx/jibx-run/1.3.1/" includes="*.jar" /> <fileset dir="D:/develop/apache-maven/repository-pub/org/apache/bcel/bcel/6.0/" includes="*.jar" /> </path> <!--这个是主任务 , depends 依赖下面写的三个分任务 --> <target name="main" depends="compile,bindgen,bind" description="Main target" /> <target name="compile" description="Compilation target"> <echo>Building file.</echo> <!--相当于运行 javac命令进行源码编译--> <javac srcdir="${basedir}/src/main/java" destdir="${basedir}/target/classes" includeantruntime="true" /> </target> <target name="bindgen"> <echo message="Running BindGen tool" /> <!-- 相当于运行Java命令生成binding.xml文件 类似于网上说的如下命令 -> java -cp ..libx-tools.jar ..BindGen -t 生成文件保存地址 -v 需要绑定文件的class文件 完整包名.类名 --> <java classpathref="classpath" fork="true" failοnerrοr="true" classname="org.jibx.binding.BindingGenerator"> <!-- 此处写需要生成映射文件的实体类的全类名--> <arg value="com.lxl.netty.httpxml.pojo.Address" /> <arg value="com.lxl.netty.httpxml.pojo.Customer" /> <arg value="com.lxl.netty.httpxml.pojo.Order" /> <arg value="com.lxl.netty.httpxml.pojo.Shipping" /> </java> </target> <target name="bind"> <!--将实体类的class和xml映射文件进行绑定--> <echo message="Running bind" /> <taskdef name="bind" classname="org.jibx.binding.ant.CompileTask"> <classpath refid="classpath"/> </taskdef> <bind binding="${basedir}/binding.xml"> <classpath refid="classpath"/> </bind> </target> </project>
3, 以上步骤做完后,就可以运行ant编译任务了.本人是在idea中运行的也在此记录下来
idea窗口右侧有一个小蜘蛛ant图标,点开,然后点加号,把自己写好的build.xml加进去
选中main然后点运行
看到有报错:
找不到目录,那就在项目目录下创建,target和classes目录
再次运行main:
可以看到生成了binding.xml和jibx相关的class文件
至此jibx相关的class就生成成功 了.为了验证可以再写一个测试类测试一下,代码如下:
public class TestJibx { private IBindingFactory factory = null; private StringWriter writer = null; private StringReader reader = null; private Address bean = null; @Before public void init() { bean = new Address(); bean.setCountry("china"); bean.setPostCode("056400"); bean.setState("aaa"); try { factory = BindingDirectory.getFactory(Address.class); } catch (JiBXException e) { e.printStackTrace(); } } @After public void destroy() { bean = null; try { if (writer != null) { writer.flush(); writer.close(); } if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } @Test public void bean2xml() { writer = new StringWriter(); // marshal编组 try { IMarshallingContext mctx = factory.createMarshallingContext(); mctx.setIndent(2); mctx.marshalDocument(bean,"urf-8",null,writer); fail(writer); reader = new StringReader(writer.toString()); // unmarshal 解组 IUnmarshallingContext uctx = factory.createUnmarshallingContext(); Address address = (Address) uctx.unmarshalDocument(reader, null); failRed(address); } catch (Exception e) { e.printStackTrace(); } } public void fail(Object o) { System.out.println(o); } public void failRed(Object o) { System.err.println(o); } }
运行,居然报错了:
无法访问绑定信息,这个可能是我先运行的ant的任务然后才写的测试类代码,导致class没有和jibx的映射绑定的原因.继续解决吧.
我把 classes下的文件和binding.xml都删除了,然后先运行测试的代码生成了class文件,然后再次运行ant的main任务.此时class就和jibx的xml绑定好了.
再次运行测试代码:
完美,可以接着玩netty和http+xml了 :)