下面列出了怎么用com.fasterxml.jackson.databind.util.JSONPObject的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType(CONTENT_TYPE);
if (allowedOrigin != null) {
resp.setHeader("Access-Control-Allow-Origin", allowedOrigin);
}
resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
resp.setStatus(HttpServletResponse.SC_OK);
final OutputStream output = resp.getOutputStream();
try {
if (jsonpParamName != null && req.getParameter(jsonpParamName) != null) {
getWriter(req).writeValue(output, new JSONPObject(req.getParameter(jsonpParamName), registry));
} else {
getWriter(req).writeValue(output, registry);
}
} finally {
output.close();
}
}
@Override
protected void doGet(final HttpServletRequest req,
final HttpServletResponse resp) throws IOException {
resp.setContentType(CONTENT_TYPE);
if (allowedOrigin != null) {
resp.setHeader("Access-Control-Allow-Origin", allowedOrigin);
}
resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
resp.setStatus(HttpServletResponse.SC_OK);
final OutputStream output = resp.getOutputStream();
try {
if (jsonpParamName != null && req.getParameter(jsonpParamName) != null) {
getWriter(req).writeValue(output, new JSONPObject(req.getParameter(jsonpParamName), registry));
} else {
getWriter(req).writeValue(output, registry);
}
} finally {
output.close();
}
}
/**
* TODO: replace JSONWithPadding to JSONPObject.
*
* @param callback String
* @param typeId long
* @param q String
* @return JSONPObject
*/
@GET
@Path("search")
@Produces(MediaType.APPLICATION_JSON)
public JSONPObject getPartyEntitiesByType(
@QueryParam("callback") String callback,
@QueryParam("typeId") long typeId, @QueryParam("q") String q) {
String hql = "from PartyEntity where partyType.id=? and name like ? order by name";
Page page = partyEntityManager.pagedQuery(hql, 1, DFAULT_PAGE_SIZE,
typeId, q.replace("_", "\\_") + "%");
List<PartyEntity> partyEntities = (List<PartyEntity>) page.getResult();
List<PartyEntityDTO> partyEntityDtos = new ArrayList<PartyEntityDTO>();
for (PartyEntity partyEntity : partyEntities) {
PartyEntityDTO partyEntityDto = new PartyEntityDTO();
partyEntityDto.setId(partyEntity.getId());
partyEntityDto.setName(partyEntity.getName());
partyEntityDtos.add(partyEntityDto);
}
return new JSONPObject(callback, partyEntityDtos);
}
@RequestMapping(value = "/jsonpInfo", method = {RequestMethod.GET})
@ResponseBody
public Object jsonpInfo(String callback, String userId) throws IOException {
UUser user = userFService.getUserById(userId);
JSONPObject jsonpObject = new JSONPObject(callback, user);
return jsonpObject;
}
@Override
public void toJsonP( OutputStream output, Object value, String callback )
throws IOException
{
if ( StringUtils.isEmpty( callback ) )
{
callback = "callback";
}
jsonMapper.writeValue( output, new JSONPObject( callback, value ) );
}
@Override
protected void exceptionResponse(HttpServletRequest request, HttpServletResponse response, APIException e) throws IOException {
ErrorBean bean = new ErrorBean(e);
String jsonpCallback = request.getParameter("jsonpCallback");
final ObjectMapper objectMapper = new ObjectMapper();
response.setContentType("application/json");
if (StringUtils.isBlank(jsonpCallback)) {
objectMapper.writeValue(response.getOutputStream(), bean);
} else {
JSONPObject jsonp = new JSONPObject(jsonpCallback, bean);
objectMapper.writeValue(response.getOutputStream(), jsonp);
}
}
@ExceptionHandler(value = APIException.class)
public @ResponseBody
JSONPObject handleException(APIException ex, HttpServletRequest request) {
String jsonpCallback = request.getParameter("jsonpCallback");
if (StringUtils.isBlank(jsonpCallback)) {
jsonpCallback = "jsonpCallback";
}
return asCallback(jsonpCallback, new ErrorBean(ex));
}
@RequestMapping("/action/new")
public
@ResponseBody
JSONPObject registerAction(HttpSession session,
@RequestParam("user") String userId,
@RequestParam("item") String itemId,
@RequestParam("type") Integer type,
@RequestParam("jsonpCallback") String callback,
@RequestParam(value = "source", required = false) String referrer,
@RequestParam(value = "rectag", required = false) String recTag,
@RequestParam(value = "pos", required = false) Integer pos,
@RequestParam(value = "rlabs", required = false) String rlabs,
@RequestParam(value = "extra_data", required = false) String extraData,
@RequestParam(value = "click_only", required = false) Boolean click_only,
@RequestParam(value = "zehtg", required = false) String req) {
final ConsumerBean consumerBean = retrieveConsumer(session);
MDCKeys.addKeys(consumerBean, userId, itemId,recTag);
//added zehtg parameter as additional option for rlabs
if(StringUtils.isNotBlank(req)) { rlabs=req; }
if (logger.isDebugEnabled())
logger.debug("Creating action for consumer: " + consumerBean.getShort_name());
ActionBean actionBean = createAction(userId, itemId, type, referrer,recTag,extraData);
boolean isCTR = StringUtils.isNotBlank(rlabs);
boolean clickOnly = (isCTR && (click_only != null) && (click_only == true)) ? true : false;
int clickPos = -1;
if (pos != null)
clickPos = pos.intValue();
return asCallback(callback, actionBusinessService.addAction(consumerBean, actionBean, isCTR, rlabs,recTag,clickPos, clickOnly));
}
@RequestMapping("/user/new")
public @ResponseBody
JSONPObject registerUser(HttpSession session,
@RequestParam("user") String userId,
@RequestParam(value = "username", required = false) String username,
@RequestParam("jsonpCallback") String callback) {
username = (username != null) ? username : userId;
final ConsumerBean consumerBean = retrieveConsumer(session);
UserBean user = new UserBean(userId, username);
user.setType(1);
logger.debug("Creating user: " + userId + " for consumer: " + consumerBean.getShort_name());
ResourceBean responseBean = userBusinessService.updateUser((ConsumerBean) consumerBean, user, null, false, false);
return asCallback(callback, responseBean);
}
@RequestMapping("/user/profile")
public @ResponseBody
JSONPObject userProfile(HttpSession session,
@RequestParam("user") String userId,
@RequestParam(value = "models", required = false) String models,
@RequestParam("jsonpCallback") String callback) {
final ConsumerBean consumerBean = retrieveConsumer(session);
if (logger.isDebugEnabled())
logger.debug("get user profile: " + userId + " for consumer: " + consumerBean.getShort_name());
ResourceBean responseBean = userProfileService.getProfile(consumerBean, userId, models);
return asCallback(callback, responseBean);
}
@RequestMapping("/event/new")
public
@ResponseBody
JSONPObject registerEvent(HttpSession session,
HttpServletRequest request,
@RequestParam("jsonpCallback") String callback) {
final ConsumerBean consumerBean = retrieveConsumer(session);
@SuppressWarnings("unchecked")
Map<String,String[]> parameters = request.getParameterMap();
return asCallback(callback, predictionBusinessService.addEvent(consumerBean, parameters));
}
@RequestMapping("/predict")
public
@ResponseBody
JSONPObject predict(HttpSession session,
HttpServletRequest request,
@RequestParam(value = "json", required = false) String json,
@RequestParam("jsonpCallback") String callback) {
final ConsumerBean consumerBean = retrieveConsumer(session);
@SuppressWarnings("unchecked")
Map<String,String[]> parameters = request.getParameterMap();
return asCallback(callback, predictionBusinessService.predict(consumerBean, parameters));
}
/**
* 输出JSONP格式数据.
*/
public String toJsonpString(String functionName, Object object) {
return toJsonString(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonp(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 输出JSONP格式数据.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
public String toJsonPadding(String function, Object object){
return toJson(new JSONPObject(function, object));
}
public String toJsonPadding(Object object){
return toJson(new JSONPObject(DEFAULT_JSONP_NAME, object));
}
private JSONPObject asCallback(String callbackName, Object valueObject) {
return new JSONPObject(callbackName, valueObject);
}