使用JSON的Jersey RESTfull服务 [英] Jersey RESTfull service with JSON

查看:259
本文介绍了使用JSON的Jersey RESTfull服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JSON-Post中的值解析为Java-Variables。但它们总是空的!

I want to parse the values from the JSON-Post into Java-Variables. But they are always empty!

JSON-Post:

JSON-Post:

{"algID":0,"vertices":[1,2,3]}

我尝试将其解析为Java-Variables:

My try to parse it into Java-Variables:

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Path("getCloseness_vertices")
  public String getCloseness_vertices(
  @FormParam("algID") int algID,
  @FormParam("vertices") IntArray vertices)

如果我这样尝试:

public String getCloseness_vertices(int algID)

Tomcat说:


Java类int的消息体读取器,Java类型int和MIME
媒体类型application / json; charset =找不到UTF-8。

A message body reader for Java class int, and Java type int, and MIME media type application/json; charset=UTF-8 was not found.

任何帮助都会很好,我只是不明白。

Any help would be nice, I just don't get it.

推荐答案

你需要创建一个可以将JSON序列化为JSON的POJO:

You need to create a POJO that Jersey can serialize the JSON to:

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MyPojo {
    public int algID;
    public int[] verticies;

    public MyPojo() {} // constructor is required

}

然后......

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("getCloseness_vertices")
public String getCloseness_vertices(MyPojo p) 
{
    int i = p.algID;
}

此外,您还需要包含 jersey- json jar文件。

Also you need to include the jersey-json jar file.

这篇关于使用JSON的Jersey RESTfull服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆