这里我试图从其中一个服务返回JSON,但它往往会出错:
SEVERE: The registered message body writers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
这是我的计划:
import org.apache.solr.common.SolrDocumentList;
@POST
@Path("/userQuery")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public SolrDocumentList userQuery(String p){
int sizy;
String stry;
StringBuilder sb = new StringBuilder();
SolrDocument doc = null;
SolrDocumentList docList = null;
List<String> arr = new ArrayList();
StringTokenizer token = new StringTokenizer(p,",");
while(token.hasMoreElements()){
stry = token.nextToken();
arr.add(stry);
}
Set<String> xrr = new HashSet<String>(arr);
try {
SolrQuery query = new SolrQuery(createQuery(arr));
query.setIncludeScore(false);
query.setFields("age_group,last_name,fav_count,first_name");
query.setParam("wt", "json");
System.out.println(query);
QueryResponse qr = authorSearcher.query(query);
if (qr != null) {
docList = qr.getResults();
}
} catch (SolrServerException ex) {
sLog.error();
}
return docList;
}
这里docList以JSON格式给出输出,注意param:wt = json。我在码头上使用简单的JAX-rs jersey app。 如果我尝试将docList作为字符串返回,则返回一个期望的结果,但JSON返回失败。
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Multiple packages, separated by semicolon(;), can be specified in param-value</description>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>cribservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>