简体   繁体   English

球衣是如何产生波动的,有没有解决方案

[英]How jersey generate the wadl, are there a scheme for this

I want to parse a .wadl file which is generated by jersey. 我想解析一个由jersey生成的.wadl文件。 but I have no idea that how jersey generate the wadl file, and is there a scheme file used by jersey. 但是我不知道jersey如何生成wadl文件,并且jersey是否使用了方案文件。 In fact ,I want to find the scheme file and use xmlBeans to parse the wadl file. 实际上,我想找到方案文件并使用xmlBeans来解析wadl文件。 I want to know is there any other method to do this. 我想知道还有其他方法可以做到这一点。 I will be appreciate! 我将不胜感激!

The easiest thing to do is look at the resource class that generated the wadl response . 最简单的方法是查看生成wadl响应资源类 You'll see that model class it uses as the representation , is that com.sun.research.ws.wadl.Application , which is a JAXB generated class. 您将看到它用作表示形式的模型类com.sun.research.ws.wadl.Application ,它是JAXB生成的类。 If you look at the containing package , you will see all the other model classes the make up the complete wadl model. 如果查看包含的包 ,您将看到所有其他模型类,它们构成了完整的wadl模型。

So since these are JAXB classes, you can just use JAXB to unmarshal the response using the Application class as the root model type. 因此,由于这些是JAXB类,因此您可以使用Application类作为根模型类型来使用JAXB解组响应。

If you are using the Jersey client, you could then unmarshal straight to Application from the Response 如果您使用的是Jersey客户端,则可以直接从Response解组到Application

Response response = target("application.wadl").request("application/xml").get();
Application wadlApp = response.readEntity(Application.class);

Or if you get a String or InputStream some other way, you can use JAXB directly to unmarshal 或者,如果您以其他方式获取StringInputStream ,则可以直接使用JAXB来解组

String wadlString = ...
JAXBContext context = JAXBContext.newInstance(Application.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Application wadlApp = (Application)unmarshaller.unmarshal(new StringReader(wadlString));

The problem with this approach is that the classes are contained in the jersey-server artifact. 这种方法的问题在于,这些类包含在jersey-server工件中。 You might not want this load for whatever you are doing. 您可能不希望此负载为正在执行的任何操作。

You can also try to use xjc to generate the JAXB classes yourself, with the wadl.xsd Jersey uses. 您也可以尝试使用xjc自己通过wadl.xsd Jersey的使用来生成JAXB类。 I would imagine you get similar if not the same model classes used by Jersey. 我想您会得到与Jersey所使用的模型类不同的模型。 This way you don't need to depend on the jersey-server . 这样,您无需依赖jersey-server


UPDATE 更新

So I just tested with xjc to generate the JAXB classes, using the above wadl.xml, and it works. 因此,我刚刚使用上面的wadl.xml对xjc进行了测试,以生成JAXB类,并且它可以工作。 It produces the same classes used by Jersey 它产生与Jersey相同的类

// maven directory structure (src/main/java)
xjc -d src/main/java -p com.stackoverflow.jersey.wadl.test wadl.xsd

Then you can just use the JAXB example above to unmarshal using the generated classes. 然后,您可以仅使用上面的JAXB示例来解组使用生成的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM