RESTful Webservice - 如何提供自定义类? xml 输出中的 .xsd
我已经搜索了大约一天半,但找不到任何有用的答案...... (无论如何,我知道我绝对知道如何编写网络服务:D)
这是我的情况: 我正在尝试设置一个 RESTful Web 服务环境,该环境提供返回一个对象名称 Greeting:
Greeting.class
@XmlRootElement(name = "greeting")
public class Greeting {
private String message;
private String name;
public Greeting() {
}
public Greeting(String message, String name) {
this.message = message;
this.name = name;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Web 服务仅返回一个 Greeting 实例...仅此而已!
GreetingService.class
@Path("sayHello")
public class GreetingService {
@Context
private UriInfo context;
public GreetingService() {
}
@GET
@Produces("application/xml")
public Greeting getJson(@QueryParam("name") String name) {
return new Greeting(getGreeting(), name);
}
private String getGreeting() {
return "Good " + (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM ? "Morning" : "Afternoon");
}
}
浏览器中的结果是这样的 浏览器
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<greeting>
<message>Good Afternoon</message>
</greeting>
没有给出有关 Greeting.class 的详细结构的信息:( 但是与 Web 服务通信的其他客户端可能需要它(是的,这很简单 - 但这只是一个示例!)
我需要配置什么吗?也许是 application.wadl?我不知道 - 就像我说的那样在谷歌上搜索 - 将近两天:( 请帮我! 顺便说一句 - 我正在使用 Netbeans、Java 7、JEE6 和 Jersey
编辑
也许我没有问正确...... 我如何自动提供问候语所需的 .xsd 架构?
这是目前提供的 application.wadl...
<application xmlns="http://research.sun.com/wadl/2006/10">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.8 06/24/2011 12:17 PM"/>
<resources base="http://localhost/iOSWebServices/resources/">
<resource path="greeting">
<method id="greet" name="GET">
<response>
<representation mediaType="application/xml"/>
</response>
</method>
<method id="putXml" name="PUT">
<request>
<representation mediaType="application/xml"/>
</request>
</method>
</resource>
</resources>
</application>
我发现,响应表示中必须有一个参数“元素” - 我如何在那里设置它?是的,我可以使用 Netbeans 的 WADL 插件并手动输入 - 但我必须为元素添加架构引用 - 在本例中为“Greeting” - 而且我仍然不知道如何设置 Greeting 的命名空间。正确上课:(
编辑
我可能已经找到了我正在寻找的东西..
I've been searching like one and a half days but I can't find any useful answer...
(anyway, know I definetly know how to write webservices :D)
Here's my situation:
I am triying to setup a RESTful Webservices environment that provides returns an object names Greeting:
Greeting.class
@XmlRootElement(name = "greeting")
public class Greeting {
private String message;
private String name;
public Greeting() {
}
public Greeting(String message, String name) {
this.message = message;
this.name = name;
}
@XmlElement
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The Webservice just returns an instance of Greeting... thats all!
GreetingService.class
@Path("sayHello")
public class GreetingService {
@Context
private UriInfo context;
public GreetingService() {
}
@GET
@Produces("application/xml")
public Greeting getJson(@QueryParam("name") String name) {
return new Greeting(getGreeting(), name);
}
private String getGreeting() {
return "Good " + (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM ? "Morning" : "Afternoon");
}
}
And the result in the browser is this
Browser
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<greeting>
<message>Good Afternoon</message>
</greeting>
There is no information given about the detailed structure of Greeting.class :(
But other clients, who talk to the webservice might need it (yeah it's simple - BUT it's just an example!)
Do I have to configure anything? Maybe the application.wadl? I've no idea - was googling like I said - nearly two days :(
Please help me!
BTW - I am using Netbeans, Java 7, JEE6 and Jersey
EDIT
Maybe I didn't ask correctly...
How can i automaticly provide the needed .xsd schema for Greeting?
Here's the application.wadl, which is provides at the moment...
<application xmlns="http://research.sun.com/wadl/2006/10">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.8 06/24/2011 12:17 PM"/>
<resources base="http://localhost/iOSWebServices/resources/">
<resource path="greeting">
<method id="greet" name="GET">
<response>
<representation mediaType="application/xml"/>
</response>
</method>
<method id="putXml" name="PUT">
<request>
<representation mediaType="application/xml"/>
</request>
</method>
</resource>
</resources>
</application>
I found out, that there has to be a param "element" in the response representation - HOW can I set it there? Yeah I could use the WADL Plugin for Netbeans and type it in manually - but I have to add a schema reference for the element - in this case "Greeting" - and I still don't know how to set the namespace for the Greeting.class correctly :(
EDIT
I might have found what I was looking for..
http://www.verborgh.be/articles/2009/11/21/easy-restfull-jax-rs-webservices-and-extended-wadl-on-glassfish-v3-using-ant-/
This article explains what I wanted to know :)
Still have one exception in the last step when overriding the WADL generation but I think, this is a version bug - gonna try to update jersey or to find a workaround!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您使用的是 NetBeans IDE,以下内容可能会有所帮助:
Since you are using NetBeans IDE, the following may help:
有一个 JAXB 模式生成工具
schemagen
可以自动为 java 类生成 XSD。请参阅 http://download.oracle.com/javaee/5/tutorial/ doc/bnbah.html
There is a JAXB schema generation tool
schemagen
that can automatically generate an XSD for a java class.See http://download.oracle.com/javaee/5/tutorial/doc/bnbah.html
我可能已经找到了我正在寻找的东西.. http://www.verborgh.be/articles/2009/11/21/easy-restfull-jax-rs-webservices-and-extended-wadl-on-glassfish-v3-using-ant- / 本文解释了我想知道的内容:) 在覆盖 WADL 生成时的最后一步中仍然有一个异常,但我认为,这是一个版本错误 - 将尝试更新球衣或找到解决方法!
I might have found what I was looking for.. http://www.verborgh.be/articles/2009/11/21/easy-restfull-jax-rs-webservices-and-extended-wadl-on-glassfish-v3-using-ant-/ This article explains what I wanted to know :) Still have one exception in the last step when overriding the WADL generation but I think, this is a version bug - gonna try to update jersey or to find a workaround!