RESTful Java Web 服务中 POST 方法的问题

发布于 2024-10-01 09:48:04 字数 996 浏览 1 评论 0原文

我正在尝试创建一个实现所有四个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原野 2024-10-08 09:48:04

我认为你的问题是使用 toString() 方法获取 InputStream 的字符串的行:

String xml = is.toString();

你应该读取输入流中的数据并将其附加到一个新的字符串。 toString() 是流的字符串表示形式,但不是流的内容。您必须使用 InputStreamread 方法,并使用 StringBuilder 将字节附加到新的 String 或一个 StringWriter。

I think your problem is the line in which you use the toString() method to get the string of the InputStream:

String xml = is.toString();

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 the read methods of the InputStream and append the bytes to a new String using a StringBuilder or a StringWriter.

薆情海 2024-10-08 09:48:04

经过艰苦努力寻找答案后,我成功解决了它!这只是缺少对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

浴红衣 2024-10-08 09:48:04

我不明白为什么你没有 createStudent(Student Student) 。 jsr311 的整体思想是参数也被解析并提供给函数。

编辑
有那么一瞬间,我以为我把事情搞混了。但我在这里找到了一个示例 http://blogs.oracle.com/enterprisetechtips/entry/configuring_json_for_restful_web

@PUT
       @Consumes("application/json")
       public synchronized void setStatus(StatusInfoBean status) {
          ...
       }

所以你甚至不需要解析 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

@PUT
       @Consumes("application/json")
       public synchronized void setStatus(StatusInfoBean status) {
          ...
       }

So you don't even need to parse xml or json. This work is already done for you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文