以json格式返回solr响应(Return solr response in json format)
我试图以JSON格式返回solr响应。 但是我无法获得JSON响应。 下面是我的solr配置:'
<queryResponseWriter name="json" default="true" class="org.apache.solr.request.JSONResponseWriter"> <str name="content-type">application/json</str> </queryResponseWriter>
下面是我的java代码:
HttpSolrServer server = new HttpSolrServer(serverUrl); SolrQuery query = new SolrQuery(); query.setQuery(query); query.set("indent","true"); query.set("wt","json"); QueryResponse response = server.query(query); System.out.println(response.getResults().get(index));
但是输出以下列格式显示。
{numFound=1,start=0,docs=[SolrDocument{_uniqueKey=[“abc@gmail.com”,”abc”], calculation_policy=text_value, username=abc, email=abc@gmail.com, display_order=10, last_login=Mon Jan 26 11:27:35 PST 2015, created=Mon Jan 26 11:27:35 PST 2015}]}
但是,执行以下行后,我得到了JSON响应:
System.out.println(new Gson().toJson(queryResponse.getResults().get(index)));
有人可以告诉我,我错过了哪一步?
I am trying to return solr response in JSON format. However I am not able to get the JSON response. Below is my solr config:'
<queryResponseWriter name="json" default="true" class="org.apache.solr.request.JSONResponseWriter"> <str name="content-type">application/json</str> </queryResponseWriter>
Below is my java code:
HttpSolrServer server = new HttpSolrServer(serverUrl); SolrQuery query = new SolrQuery(); query.setQuery(query); query.set("indent","true"); query.set("wt","json"); QueryResponse response = server.query(query); System.out.println(response.getResults().get(index));
However output is displyed in following format.
{numFound=1,start=0,docs=[SolrDocument{_uniqueKey=[“abc@gmail.com”,”abc”], calculation_policy=text_value, username=abc, email=abc@gmail.com, display_order=10, last_login=Mon Jan 26 11:27:35 PST 2015, created=Mon Jan 26 11:27:35 PST 2015}]}
However I get JSON response after execute following line:
System.out.println(new Gson().toJson(queryResponse.getResults().get(index)));
Can someone please tell me what step am I missing?
原文:https://stackoverflow.com/questions/28374428
最满意答案
您没有使用泛型(您使用的是原始类型 ),因此编译器不知道映射中的值是什么类型 -
map.get(k)
返回Object
,就编译器而言, no+(Object, int)
运算符。只需在
map
声明和初始化中使用泛型:// Java 7 and later, using the "diamond operator" Map<Integer, Integer> map = new HashMap<>();
要么:
// Java 5 and 6 (will work with 7 and later too, but is longer) Map<Integer, Integer> map = new HashMap<Integer, Integer>();
其余的将编译没有问题。
如果您是泛型新手 ,请参阅教程和综合FAQ 。 你应该很少使用原始类型(几乎不会)。 你应该能够配置你的IDE或者编译器给你一个警告,如果你这样做的话(例如用
-Xlint
forjavac
)。You're not using generics (you're using a raw type instead), so the compiler has no idea what type of value is in the map -
map.get(k)
returnsObject
as far as the compiler is concerned, and there's no+(Object, int)
operator.Just use generics in your
map
declaration and initialization:// Java 7 and later, using the "diamond operator" Map<Integer, Integer> map = new HashMap<>();
Or:
// Java 5 and 6 (will work with 7 and later too, but is longer) Map<Integer, Integer> map = new HashMap<Integer, Integer>();
The rest will compile with no problems.
If you're new to generics, see the tutorial and the comprehensive FAQ. You should very, very rarely use raw types (almost never). You should be able to configure your IDE or compiler to give you a warning if you do so (e.g. with
-Xlint
forjavac
).
相关问答
更多-
您所需的代码可能如下所示: public static void main (String[] args) throws java.lang.Exception { int arr [] = {3,4,5,6,7}; IntStream.of(arr) .filter(i -> i % 2 == 0) .sorted() .map(j -> j+ 1) .forEach(System.out::println) ...
-
你可以通过添加toString() Methode来解决这个问题。 为什么需要toString() Methode? 因为它会含糊不清。 当Java知道至少有一个部分应该是一个字符串时,它只会将+转换为String#append。 - @Silverclaw 更新的代码: enum Paths { STYLE_SHEETS("../stylesheets"), CONTROLLER("../controller"), CONNECTION("../connection"), RESOURCE("../res ...
-
您需要使用关系运算符==而不是=。 if语句的括号内的3个子语句中的每一个都应该表示一个布尔值。 在Java中,=用于分配值,==用于检查相等性。 因此,您必须将if语句更改为: if((board[z][i] == 1) && (board[z][i++] == 1) && (board[z++][i] == 1)){ 此外,不是将z和i递增1(因为你已经将它作为for循环的一部分),也许使i ++ - > i + 1和z ++ - > z + 1。 希望这可以帮助! You need to use t ...
-
Java错误:HashMap中二元运算符的操作数类型错误[复制](Java error: bad operand types for binary operator in HashMap [duplicate])[2022-07-14]
你错过了想用来参数化地图的类型。 它应该是: HashMapdict = new HashMap<>(); You missed types you wanted to use to parametrize the map. It should be: HashMap dict = new HashMap<>(); -
Java:显示错误的地图:二元运算符的错误操作数类型“+”(Java : Map showing error : bad operand type for binary operator “+”)[2023-06-22]
您没有使用泛型(您使用的是原始类型 ),因此编译器不知道映射中的值是什么类型 - map.get(k)返回Object ,就编译器而言, no +(Object, int)运算符。 只需在map声明和初始化中使用泛型: // Java 7 and later, using the "diamond operator" Mapmap = new HashMap<>(); 要么: // Java 5 and 6 (will work with 7 and later to ... -
char是基本类型, Random是引用类型。 您无法使用<,>,==或!=运算符自由地比较它们。 从我在您的代码中看到的,您实际上从未获得用户的输入。 您应该使用扫描仪从标准输入读取并将读取值与i进行比较。 char is a primitive type and Random is a reference type. You can't compare them freely using the <, >, == or != operators. From what I see in your code ...
-
~=在Swift标准库中定义为期望左侧的模式和右侧的值。 声明是: public func ~=(pattern: Range, value: I) -> Bool 你可以添加你自己的版本,它支持左边的值和右边的范围,如果你喜欢这样的东西: func ~=(value: I, pattern: Range) -> Bool ...
-
您无法将ArrayList添加到int。 totalIC = totalIC + l.getInventory(); ^ ^ ^ int int ArrayList containing Lamborghini-Objects 我假设你想知道清单中包含多少兰博基尼物体。 要这样做,你会打电话 l.getInventory().size(); You can not add an ArrayList to an int. totalI ...
-
Java calulator抛出二进制运算符的'-bad操作数类型'-'-(Java calulator throws -bad operand types for binary operator '-'-)[2022-07-27]
你需要括号: System.out.println("The answer is " + (cal1-cal2) + "."); 否则你所拥有的被视为 System.out.println(("The answer is " + cal1) - (cal2 + ".")); 这是无效的,因为你不能减去字符串。 为什么你没有与其他运营商的错误? 好吧, *和/有更高的优先级,所以那些正在按预期工作。 另一方面, +重载以连接字符串: System.out.println("The answer is " ... -
几个错误,需要纠正。 虽然equalsIgnoreCase足以匹配candy或Candy或CANDY ,**字candy任何大写/小写 1.) c = "C" 是赋值而非比较 。 2.) c在这里是char ,所以需要将char与char进行比较 , 而不是将char与String进行比较 。 所以写c == 'C' 代码应如下所示。 码 public static void main(String[] args) { System.out.println("Guess a word or ...