是否可以在我的代码

时间:2015-05-21 14:24:49

标签: java jersey cache-control

我有一个简单的JERSEY WEBService,负责返回数据库中的状态

package com.services.regg;
@Path("/getstates")
public class FetchStates {
    @GET
    @Consumes("application/text")
    @Produces("application/json")
    public String getAllSates() throws JSONException
    {
        JSONArray jsonarray_allstates = new JSONArray();
        Connection dbConnection = null;
        PreparedStatement getAllStatesPst = null ;
        ResultSet getAllStatesResltSet = null;
        try
        {
            dbConnection = DBConnectionOrient.getDBConnection();
            getAllStatesPst = dbConnection.prepareStatement("select stateID , stateName from tbl_state ORDER BY stateName");
            getAllStatesResltSet = getAllStatesPst.executeQuery();
            while(getAllStatesResltSet.next())
            {
                JSONObject eachrecordjsonObj = new JSONObject();
                String stateID = getAllStatesResltSet.getString("stateID").trim();
                String stateName = getAllStatesResltSet.getString("stateName").trim();
                eachrecordjsonObj.put("stateID", stateID).put("stateName", stateName);
                if(!stateName.isEmpty() && !stateName.equals(""))
                {
                jsonarray_allstates.put(eachrecordjsonObj);
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            DBConnectionOrient.close(getAllStatesPst,getAllStatesResltSet);
            DBConnectionOrient.close(dbConnection);
        }
        String  response = "jsonCallback("+jsonarray_allstates.toString()+")";
        return response;
    }
}

这一切都很好。

由于这适用于移动应用程序,我们看到来自后端的响应非常缓慢

我想从我的方面优化应用程序的性能。 为此我看过Jersey Cache Control。

CacheControl cc = new CacheControl();
cc.setMaxAge(86400);
cc.setPrivate(true);

ResponseBuilder builder = Response.ok(myBook);
builder.cacheControl(cc);
return builder.build();

我的问题是,是否可以在我的代码中使用CacheControl,因为我直接发送字符串而不是ResponseBuilder

如果有,请告诉我,如何修改我的代码以使用缓存控制?

1 个答案:

答案 0 :(得分:0)

只需重新构建代码即可返回Response而不是String。在使用Response.ok(myBook);的情况下,只需将您的jsonp response传递给myBook而不是String

唯一的其他方法是直接操纵标题。

如果必须坚持使用HttpSerlvetResponse返回类型,那么您可以注入@GET public String getAllSates(@Context HttpServletResponse response) { CacheControl cc = new CacheControl(); cc.setMaxAge(86400); cc.setPrivate(true); response.addHeader("Cache-Control", cc.toString()); [...] } 并只设置标题

ContainerResponseFilter

或者您可以在CXX=g++-4.8 CXXFLAGS=-std=c++11 -pedantic -Wall -pedantic-errors -Wextra IPATH=-I/usr/include/qt4/ -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql LDFLAGS=-lQtGui -lQtSql -lQtCore OBJS=main.o HEADERS=mymoc1.hpp mymoc2.hpp other-headers all: myexec myexec: $(OBJS) $(CXX) $(CXXFLAGS) $(OBJS) -o $@ $(LDFLAGS) main.o: main.cpp $(HEADERS) $(CXX) -c $(CXXFLAGS) $< -o $@ $(IPATH) mymoc1.hpp: the-header-needing-moc.hpp moc $< -o $@ mymoc2.hpp: other-header-needing-moc.hpp moc $< -o $@ 内添加标题,如果您不想在每种方法中添加标题。不确定您使用的是哪个泽西岛版本,但是使用Jersey 2.x,您可以确定哪种方法将使用过滤器,以便只有选定的资源才能通过此过滤器。如果此功能可用,不确定泽西岛1.x.这是2.x的the documentation

相关问题