使用JSON的Jersey RESTfull服务

编程入门 行业动态 更新时间:2024-10-20 01:27:26
本文介绍了使用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服务

本文发布于:2023-10-31 06:14:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   Jersey   RESTfull

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!