简体   繁体   English

JAX-RS JSON对象转换为JavaScript

[英]JAX-RS JSON object to JavaScript

I am new to JAX-RS and trying to build a simple website interface. 我是JAX-RS的新手,正在尝试构建一个简单的网站界面。

So I have written a function returning a JSON object like this: 因此,我编写了一个返回JSON对象的函数,如下所示:

@GET
@Path("/mypath")
@Produces (Mediatype.APPLICATION_JSON)
public String returnJson() {
    String json = //.... fill String 
    return json;
}

which works well when browsing to this path. 浏览到此路径时效果很好。

On the other hand I have a UI page like this: 另一方面,我有一个这样的UI页面:

@GET
Produces(MediaType.TEXT_HTML)
public InputStream viewUI() throws FileNotFoundException {
    File page = new File("page.html");
    return new FileInputStream(page);
}

which works also. 也可以。

Next thing I want to do is filling a dropdown list in my page.html with JavaScript, which also should not be a problem. 接下来,我想用JavaScript填充page.html的下拉列表,这也不是问题。

But I dont know how to get the JSON object to the JavaScript array (in page.html ). 但是我不知道如何将JSON对象获取到JavaScript数组(在page.html )。

First of all, when using jaxrs, you don't need to convert objects to json. 首先,在使用jaxrs时,您不需要将对象转换为json。 This is done automatically by jaxrs. jaxrs自动完成此操作。 Your method should return an object. 您的方法应返回一个对象。 As you asking to convert json into array, I assume, your method should return a List. 我假设当您要求将json转换为数组时,您的方法应返回一个List。 Regarding of how to call and consume results from the rest service, as per Luts Horn comment, you need to use some sort of client side library, for example jquery. 关于如何调用和使用其余服务的结果,根据Luts Horn的注释,您需要使用某种客户端库,例如jquery。

You can look here http://www.tutorialspoint.com/jquery/jquery-ajax.htm 您可以在这里查看http://www.tutorialspoint.com/jquery/jquery-ajax.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM