RESTful Java Web 服务中 POST 方法的问题
我正在尝试创建一个实现所有四个 CRUD 操作的 RESTful Web 服务。我陷入了“创建”,因为不知何故我无法获取发布的数据。这是我写的方法。作为一个信息,我使用 JAX-RS + JAXB XML 的 Jersey RI 来序列化对象。
这是:
@POST
@Consumes("application/xml")
public Response createStudent(InputStream is) {
Student st = this.readStudent(is);
st.setUserId(idCounter.incrementAndGet());
studentDB.put(st.getUserId(), st);
System.out.println("Created student " + st.getUserId());
return Response.created(URI.create("/student/"+ st.getUserId())).build();
}
readStudent 方法如下:
protected Student readStudent(InputStream is){
JAXBContext ctx;
Student st = null;
try {
String xml = is.toString();
System.out.println(xml);
ctx = JAXBContext.newInstance(Student.class);
st = (Student) ctx.createUnmarshaller().unmarshal(new StringReader(xml));
return st;
} catch (JAXBException e){
e.printStackTrace();
}
finally { return st; }
}
有人能给我一些关于这个的说明吗?我用尽了所有的想法来让它发挥作用,但没有任何效果!
提前致谢。
I'm trying to make a RESTful Web Service which implements all four CRUD operations. I got stuck in "Create" because somehow I'm not able to obtain the posted data. Here is the method I wrote. Just as a info, I'm using Jersey RI of JAX-RS + JAXB XML to serialize objects.
Here it is:
@POST
@Consumes("application/xml")
public Response createStudent(InputStream is) {
Student st = this.readStudent(is);
st.setUserId(idCounter.incrementAndGet());
studentDB.put(st.getUserId(), st);
System.out.println("Created student " + st.getUserId());
return Response.created(URI.create("/student/"+ st.getUserId())).build();
}
And the readStudent method is below:
protected Student readStudent(InputStream is){
JAXBContext ctx;
Student st = null;
try {
String xml = is.toString();
System.out.println(xml);
ctx = JAXBContext.newInstance(Student.class);
st = (Student) ctx.createUnmarshaller().unmarshal(new StringReader(xml));
return st;
} catch (JAXBException e){
e.printStackTrace();
}
finally { return st; }
}
Can someone give me some light about this? I exhausted every idea to make this works and nothing worked!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题是使用 toString() 方法获取 InputStream 的字符串的行:
你应该读取输入流中的数据并将其附加到一个新的字符串。 toString() 是流的字符串表示形式,但不是流的内容。您必须使用
InputStream
的read
方法,并使用StringBuilder
将字节附加到新的String
或一个 StringWriter。I think your problem is the line in which you use the
toString()
method to get the string of theInputStream
:You should read the data in the input stream and append it to a new string. The
toString()
is a string representation of the stream, but not the contents of it. You have to use theread
methods of theInputStream
and append the bytes to a newString
using aStringBuilder
or aStringWriter
.经过艰苦努力寻找答案后,我成功解决了它!这只是缺少对resteasy-jaxb-provider.jar 的依赖。如果没有这个 .jar,servlet 就无法自动从 POJO 转换为 XML,反之亦然
After sweating hard looking for a answer, I've managed to fix it! It was just a missing dependency to resteasy-jaxb-provider.jar . Without this .jar the servlet wasn't able to do the automatic conversion from POJO to XML and vice-versa
我不明白为什么你没有
createStudent(Student Student)
。 jsr311 的整体思想是参数也被解析并提供给函数。编辑
有那么一瞬间,我以为我把事情搞混了。但我在这里找到了一个示例 http://blogs.oracle.com/enterprisetechtips/entry/configuring_json_for_restful_web
所以你甚至不需要解析 xml 或 json。这项工作已经为您完成
I don't understand why you don't have
createStudent(Student student)
instead. The whole idea of jsr311 is for the parameters to also be parsed and given to the function.EDIT
For a second I thought I was confusing things. But I found an example here http://blogs.oracle.com/enterprisetechtips/entry/configuring_json_for_restful_web
So you don't even need to parse xml or json. This work is already done for you