Category Archives: java

Convert thrift java bean to JSON object

Today we have a java bean generated by Thrift with schema like:

struct T {
   1: string address

Normally we can translate the java bean to a org.json.JSON object like:


T bean = new T();
JSONObject json = new JSONObject();
Map<String, Object> dataMap = org.apache.commons.beanutils.BeanUtils.describe(bean);
for (Entry<String, Object> entry : dataMap.entrySet()) {
    json.put(entry.getKey(), entry.getValue());

// or translate with JSONObject simple
JSONObject json = new JSONObject(bean);

But for beans which generated by thrift, Both we’ll get some no-used properties prefixes with set*:



After some googled, we are get some wiki API method, see also

 * Convert the generic TBase<?, ?> entity to JSON object.
 * @param tobj
 * @author Allex Wang
 * @return
public JSONObject convertBeanToJSON(final TBase<?, ?> tobj) {
    TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
    try {
        String json = serializer.toString(tobj, "utf8");
        return new JSONObject(json);
    } catch (TException ex) {
        LOGGER.error("Convert TBase object to JSON fails: " + ex.getMessage());
    } catch (JSONException ex) {
    return null;

Now we are get the JSON entity as expected:


resin 处理 jsp, html 页面乱码问题

## Solution 1:resin安装目录下conf/app-default.xml 文件

<servlet servlet-name="resin-file" servlet-class="com.caucho.servlets.FileServlet">

PS: resin是通过 com.caucho.servlets.FileServlet 来处理静态文件的.

## Solution 2:Put the following in your `web.xml` to achieve the goal.


See also:
Unicode – How to get the characters right? – JSP/Servlet response

## Solution 3:Setting character sets in JSPs.

You use the contentType attribute of the page directive to define character sets in JSPs, as the following example shows:

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

To use multiple character sets on a single page, use the UTF-8 character set encoding, as the following example shows:

<%@ page contentType="text/html; charset=utf-8" %>