Reputation: 654
I have a Solr 4.0, which indexes data (which contains multilingual characters) in Postgres db. When I'm doing direct request to Solr through built-in Jetty, I get the correct response in form of JSON. There is a Servlet running under Tomcat 7, which is responsible for handling request to the public API. This API gets requests via HTTP, does some sort of check for the rights for doing request and then doing request to Solr using solrj and then sends the response as JSON. The problem is when I'm requesting Solr through the Servlet I always get many ????????? symbols on the places where international characters should be. All the data in DB is in UTF-8 encoding, the URIEncoding parameter in server.xml for Tomcat is UTF-8 too. This is the way I'm writing response in the Servlet:
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
{
PrintWriter writer = res.getWriter();
String encoding = req.getCharacterEncoding();
if(null == encoding)
{
req.setCharacterEncoding("UTF-8");
}
else
{
req.setCharacterEncoding(encoding);
}
res.setContentType("application/json; charset=UTF-8");
res.setCharacterEncoding("UTF-8");
String key = req.getParameter(P_KEY);
if (ps.isPartnerKey(key))
{
if (req.getMethod().equals(M_GET))
{
String query = req.getParameter(P_QUERY);
String solr_json = SolrService.getInstance().query(query);
//parse JSON for id's and get images
if (solr_json != null)
{
writer.println(solr_json);
}
}
if (req.getMethod().equals(M_POST))
{
String id = req.getParameter(P_ID);
String field = req.getParameter(P_FIELD);
String value = req.getParameter(P_VALUE);
SolrService.getInstance().partialUpdate(id, field, value);
fs.add(id, field, value, ps.getPartnerId(key));
}
}
else
{
res.sendError(HttpServletResponse.SC_FORBIDDEN);
}
writer.close();
}
And this is how I'm getting data from Solr:
public String query(String query)
{
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", query!=null ? query : "*:*");
solrQuery.set("wt", "json");
String response = "";
try
{
QueryResponse res = solr.query(solrQuery);
response = res.getResults().toString();
log.debug(response);
}
catch (SolrServerException e)
{
e.printStackTrace();
}
return response;
}
Am I doing wrong something? What is the problem may be?
Upvotes: 2
Views: 456
Reputation: 21993
You are calling getWriter to early. Call it after setting the character encoding.
Upvotes: 2