本文整理匯總了Java中org.springframework.web.servlet.view.json.MappingJackson2JsonView類的典型用法代碼示例。如果您正苦於以下問題:Java MappingJackson2JsonView類的具體用法?Java MappingJackson2JsonView怎麽用?Java MappingJackson2JsonView使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MappingJackson2JsonView類屬於org.springframework.web.servlet.view.json包,在下文中一共展示了MappingJackson2JsonView類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: contentNegotiatingViewResolver
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Bean
public ViewResolver contentNegotiatingViewResolver() {
logger.debug("Configuring the ContentNegotiatingViewResolver");
ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setViewClass(JstlView.class);
urlBasedViewResolver.setSuffix(".html");
viewResolvers.add(urlBasedViewResolver);
viewResolver.setViewResolvers(viewResolvers);
List<View> defaultViews = new ArrayList<View>();
defaultViews.add(new MappingJackson2JsonView());
viewResolver.setDefaultViews(defaultViews);
return viewResolver;
}
示例2: resolveException
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception e) {
// log記錄異常
LOGGER.error(e.getMessage(), e);
// 非控製器請求照成的異常
if (!(handler instanceof HandlerMethod)) {
return new ModelAndView("error/500");
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (WebUtils.isAjax(handlerMethod)) {
Result result = new Result();
result.setMsg(e.getMessage());
MappingJackson2JsonView view = new MappingJackson2JsonView();
view.setObjectMapper(jacksonObjectMapper);
view.setContentType("text/html;charset=UTF-8");
return new ModelAndView(view, BeanUtils.toMap(result));
}
// 頁麵指定狀態為500,便於上層的resion或者nginx的500頁麵跳轉,由於error/500不適合對用戶展示
// response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return new ModelAndView("error/500").addObject("error", e.getMessage());
}
示例3: resolveException
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, Exception e) {
HttpDownServer.LOGGER.error("rest error:",e);
ModelAndView modelAndView = new ModelAndView();
try {
ResultInfo resultInfo = new ResultInfo().setStatus(ResultStatus.ERROR.getCode())
.setMsg(ResultInfo.MSG_ERROR);
Map<String, Object> attr = JSON.parseObject(JSON.toJSONString(resultInfo), Map.class);
MappingJackson2JsonView view = new MappingJackson2JsonView();
view.setAttributesMap(attr);
modelAndView.setView(view);
} catch (Exception e1) {
e1.printStackTrace();
}
return modelAndView;
}
示例4: resolveException
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
e.printStackTrace();
String messageStr = null;
if (e instanceof BizException) {
BizException bz = (BizException) e;
messageStr = bz.getMessage();
}
if (messageStr == null || messageStr.trim().length() == 0) {
messageStr = "係統異常";
}
RestResponse restR = new RestResponse();
restR.setCode(-1);
restR.setMessage(messageStr);
if (HttpRequestUtils.isAjax(request)) {
Map model = BeanMapConvertUtil.convertBean2Map(restR);
if (logger.isInfoEnabled()) {
logger.info(">>>>>resolveException ajax model: " + model);
}
return new ModelAndView(new MappingJackson2JsonView(), model);
}
return new ModelAndView("500", "restR", restR);
}
示例5: viewResolver
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Bean
public ViewResolver viewResolver() {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
List<View> defaultViews = new ArrayList<View>();
defaultViews.add(new MappingJackson2JsonView());
resolver.setDefaultViews(defaultViews);
resolver.setOrder(0);
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/view/");
internalResourceViewResolver.setSuffix(".jsp");
internalResourceViewResolver.setOrder(1);
viewResolvers.add(internalResourceViewResolver);
resolver.setViewResolvers(viewResolvers);
return resolver;
}
示例6: contentNegotiatingViewResolver
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Bean
public ViewResolver contentNegotiatingViewResolver() {
LOGGER.debug("Configuring the ContentNegotiatingViewResolver");
ContentNegotiatingViewResolver viewResolver = new ContentNegotiatingViewResolver();
List<ViewResolver> viewResolvers = new ArrayList<>();
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setViewClass(JstlView.class);
urlBasedViewResolver.setPrefix("/WEB-INF/pages/");
urlBasedViewResolver.setSuffix(".jsp");
viewResolvers.add(urlBasedViewResolver);
viewResolver.setViewResolvers(viewResolvers);
List<View> defaultViews = new ArrayList<>();
defaultViews.add(new MappingJackson2JsonView());
viewResolver.setDefaultViews(defaultViews);
return viewResolver;
}
示例7: resolve
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@Override
public ModelAndView resolve(final J2EContext ctx, final ProfileManager manager, final String url) {
final Set<String> prompt = authorizationRequestSupport.getOidcPromptFromAuthorizationRequest(url);
if (prompt.contains(OidcConstants.PROMPT_NONE)) {
if (manager.get(true) != null) {
return new ModelAndView(url);
}
final Map<String, String> model = new HashMap<>();
model.put(OAuth20Constants.ERROR, OidcConstants.LOGIN_REQUIRED);
return new ModelAndView(new MappingJackson2JsonView(), model);
}
return new ModelAndView(new RedirectView(url));
}
示例8: handle4AsyncRequest
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
protected ModelAndView handle4AsyncRequest(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ExceptionMetadata em = ModuleExceptionResolver.resolveException(ex);
Result<Object> result = new Result<Object>();
result.setSuccess(false);
result.setCode(em.getCode());
result.setMessage(em.getMessage());
MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
jsonView.setExtractValueFromSingleKeyModel(true);
ModelAndView mav = new ModelAndView(jsonView);
mav.addObject(result);
return mav;
}
示例9: createDelegatedView
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
private static MappingJackson2JsonView createDelegatedView() {
final MappingJackson2JsonView view = new MappingJackson2JsonView();
view.setPrettyPrint(true);
view.setDisableCaching(true);
view.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
return view;
}
示例10: createDelegatedView
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
private static MappingJackson2JsonView createDelegatedView() {
final MappingJackson2JsonView view = new MappingJackson2JsonView();
view.setPrettyPrint(true);
view.setDisableCaching(true);
view.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).findAndRegisterModules();
return view;
}
示例11: adaptException2MAV
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
private ModelAndView adaptException2MAV(Exception ex) {
ModelAndView mav = new ModelAndView();
mav.setView(new MappingJackson2JsonView());
mav.addObject("exception",ex.getClass().getSimpleName());
mav.addObject("message", ex.getMessage());
return mav;
}
示例12: handleRequest
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@GetMapping(path = {"/idcard"})
public ModelAndView handleRequest(HttpServletRequest request) throws Exception {
try {
String encodedCertificate = request.getHeader(HEADER_SSL_CLIENT_CERT);
Assert.hasLength(encodedCertificate, "Unable to find certificate from request");
request.getSession().setAttribute(Constants.CERTIFICATE_SESSION_ATTRIBUTE, X509Utils.toX509Certificate(encodedCertificate));
return new ModelAndView(new MappingJackson2JsonView(), Collections.singletonMap("ok", true));
} catch (Exception e) {
this.log(e);
return new ModelAndView(new MappingJackson2JsonView(), Collections.singletonMap("ok", false));
}
}
示例13: findAllMajors2
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@RequestMapping(value="/findAllMajors2",method=RequestMethod.GET)
@ResponseBody
public List<Major> findAllMajors2() throws JsonProcessingException
{
List<Major> majorList=majorService1.findAllMajors();
/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中實現。
* ObjectMapper有多個JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介質中。
* writeValue(File arg0, Object arg1)把arg1轉成json序列,並保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1轉成json序列,並保存到arg0輸出流中。
* writeValueAsBytes(Object arg0)把arg0轉成json序列,並把結果輸出成字節數組。
* writeValueAsString(Object arg0)把arg0轉成json序列,並把結果輸出成字符串。
*/
ObjectMapper jasonObjMapper = new ObjectMapper();
String strJason=jasonObjMapper.writeValueAsString(majorList);
//return strJason;
//開始返回
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("success", Boolean.TRUE);
attributes.put("allCoursesList", majorList);
ModelAndView modelAndView1=new ModelAndView();
//set the name of view
//view.setView("/course/showAllCourses");
modelAndView1.setViewName("/major/showAllMajors");
//add the object into model and view
//view.setAttributesMap(attributes);
//modelAndView1.setView(view);
modelAndView1.addObject(majorList);
return majorList;
}
示例14: handlerUnauthenticatedException
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
@ExceptionHandler({OptionalNotPresentException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handlerUnauthenticatedException(Exception ex, NativeWebRequest request) {
if ((request.getHeader("accept").contains("application/json"))) {
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, Serializable> attributes = new HashMap<>();
attributes.put("error", HttpStatus.NOT_FOUND);
view.setAttributesMap(attributes);
return new ModelAndView(view);
} else {
return new ModelAndView("/404");
}
}
示例15: handlerExceptionMethod
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入依賴的package包/類
/**
* 全局Controller異常處理
*
* @param ex 異常
* @return 跳轉出錯頁麵
*/
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handlerExceptionMethod(Exception ex, NativeWebRequest request) {
if ((request.getHeader("accept").contains("application/json"))) {//如果不是異步請求
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, java.io.Serializable> attributes = new HashMap<>();
attributes.put("error", HttpStatus.INTERNAL_SERVER_ERROR);
if (env.getDefaultProfiles()[0].equals("development")) {
attributes.put("MSG", ex.toString());
attributes.put("Line", ex.getStackTrace()[0].getLineNumber());
attributes.put("Method", ex.getStackTrace()[0].getMethodName());
}
view.setAttributesMap(attributes);
return new ModelAndView(view);
} else {
if (env.getDefaultProfiles()[0].equals("development")) {
ModelAndView modelAndView = new ModelAndView("500");
modelAndView.addObject("MSG", ex.toString());
modelAndView.addObject("Line", ex.getStackTrace()[0].getLineNumber());
modelAndView.addObject("Method", ex.getStackTrace()[0].getMethodName());
Writer writer = new StringWriter();
//異常輸出到瀏覽器console
ex.printStackTrace(new PrintWriter(writer));
modelAndView.addObject("detailed", writer.toString());
return modelAndView;
} else {
return new ModelAndView("/500");
}
}
}