Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回列表<;列表<;地图<;字符串,字符串>&燃气轮机&燃气轮机;作为json中rest服务类的输出?_Java_Json_Rest_Jersey 2.0 - Fatal编程技术网

Java 如何返回列表<;列表<;地图<;字符串,字符串>&燃气轮机&燃气轮机;作为json中rest服务类的输出?

Java 如何返回列表<;列表<;地图<;字符串,字符串>&燃气轮机&燃气轮机;作为json中rest服务类的输出?,java,json,rest,jersey-2.0,Java,Json,Rest,Jersey 2.0,我最初编写了一个java应用程序(commandline应用程序),现在我正尝试使用jersey和jetty Embedded将其转换为web应用程序。现在,作为服务rest类的输出,我需要输出json格式的列表。有没有办法做到这一点?我对Servlet之类的东西还不熟悉。。我试着这样做: @得到 @路径(“测试”) @产生(MediaType.APPLICATION_JSON) 公共列表>>测试()抛出FileNotFoundException、IOException、GateExceptio

我最初编写了一个java应用程序(commandline应用程序),现在我正尝试使用jersey和jetty Embedded将其转换为web应用程序。现在,作为服务rest类的输出,我需要输出json格式的
列表。有没有办法做到这一点?我对Servlet之类的东西还不熟悉。。我试着这样做:
@得到
@路径(“测试”)
@产生(MediaType.APPLICATION_JSON)
公共列表>>测试()抛出FileNotFoundException、IOException、GateException、URISyntaxException{

    System.out.println("Starting the App");
    System.out.println("Locations added successfully");
    DataFile file = new CSVDataFile();
    CommandsExecutor.outputsList.add(0, file.performReadOperation(inputFilePath));

    System.out.println("Begining execution");
    List<List<Map<String, String>>> finalOutput = CommandsExecutor.executeSequentialCommands(file);
/*  for (List<Map<String, String>> tmpList : finalOutput) { 
        for (int i = 0; i < tmpList.size(); i++) {
            Map<String, String> insideMap = tmpList.get(i);
            // for (Map.Entry<String, String> entry : insideMap.entrySet())
            // {
            // System.out.println(entry.getKey() + "/" + entry.getValue());
            // }
            JSONObject obj = new JSONObject(insideMap);
            System.out.println(obj);
        }
        System.out.println("\n");
    } */
    return finalOutput;
}  
System.out.println(“启动应用程序”);
System.out.println(“成功添加位置”);
数据文件=新的CSVDataFile();
CommandsExecutor.outputsList.add(0,file.performReadOperation(inputFilePath));
System.out.println(“开始执行”);
List finalOutput=CommandsExecutor.executeSequentialCommands(文件);
/*对于(列表tmpList:finalOutput){
对于(int i=0;i
现在我得到一个类似这样的错误:无法发送响应错误500:javax.servlet.ServletException:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:MessageBodyWriter找不到媒体类型=application/json,类型=class java.util.ArrayList,genericType=java.util.List>>


你的工作是什么?提前谢谢

您的结果可能类似于:

{
    "result": 
    [
        [ 
            {
                "key1":"value1",
                "key2":"value2",
                ...
            },
            {
                "key3":"value3", 
                "key4":"value4"
                ...
            }, 
            ...
        ],
        ...
     ]
}
在您的注释代码中,您实际上已经为映射创建了内部的
JSONObject
,您只需要将它们附加到
JSONArray
,然后将这些
JSONArray
放入另一个
JSONArray
,然后将最后的
JSONArray
放入结果
JSONObject
,并返回它

JSONObject result = new JSONObject();
JSONArray array1 = new JSONArray();
for (List<Map<String, String>> tmpList : finalOutput) { 
    JSONArray array2 = new JSONArray();
    for (int i = 0; i < tmpList.size(); i++) {
        Map<String, String> insideMap = tmpList.get(i);
        JSONObject obj = new JSONObject(insideMap);
        array2.add(obj);
    }
    array1.add(array2);
}
result.append("result", array1);

return result.toString();
JSONObject result=new JSONObject();
JSONArray array1=新的JSONArray();
对于(列表tmpList:finalOutput){
JSONArray array2=新的JSONArray();
对于(int i=0;i
问题的根本原因是缺少jar。Jersey使用了Jackson,因此您需要下载Jackson罐子

下面是我制作的示例应用程序所需的所有Jackson罐子的列表:

  • jackson-annotations-2.7.0.jar
  • jackson-core-2.7.0.jar
  • jackson-databind-2.7.0.jar
  • jackson-jaxrs-1.9.13.jar
  • jackson-jaxrs-base-2.2.0.jar
  • jackson-jaxrs-json-provider-2.2.0.jar
  • jackson-xc.jar
在我的示例中,我创建了以下类:

package foo;

import java.util.*;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ListWrapper 
{
    private List<List<Map<String,String>>> list;

    public List<List<Map<String,String>>> getList()
    {
        return list;
    }

    public void setList(List<List<Map<String,String>>> list)
    {
        this.list = list;
    }
}

一旦你克服了识别Jersey和Jackson所有依赖项的困难,它们确实是非常强大的库。

我了解到,如果你正在编写一些难以理解的代码(列表),那是因为你做了一些错误的事情
    package foo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/foo")
public class Foo  {

    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public ListWrapper getList()
    {       
        ListWrapper lw = new ListWrapper();

        HashMap<String,String> map = new HashMap<String,String>();
        map.put("foo", "bar");
        map.put("hello", "world");

        ArrayList<Map<String,String>> innerList = new ArrayList<Map<String,String>>();
        innerList.add(map);

        ArrayList<List<Map<String,String>>> outerList = new ArrayList<List<Map<String,String>>>();
        outerList.add(innerList);

        lw.setList(outerList);
        return lw;
    }
}
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>foo,com.fasterxml.jackson.jaxrs.json</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
{
   "list": [
      [
         {
            "foo": "bar",
            "hello": "world"
         }
      ]
   ]
}