当前位置: 首页>>代码示例>>Java>>正文


Java MappingJacksonJsonView类代码示例

本文整理汇总了Java中org.springframework.web.servlet.view.json.MappingJacksonJsonView的典型用法代码示例。如果您正苦于以下问题:Java MappingJacksonJsonView类的具体用法?Java MappingJacksonJsonView怎么用?Java MappingJacksonJsonView使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MappingJacksonJsonView类属于org.springframework.web.servlet.view.json包,在下文中一共展示了MappingJacksonJsonView类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: doResolveException

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {
    ModelAndView mav = super.doResolveException(request, response, handler, ex);

    if (ex instanceof NoPermissionException) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        logger.info(String.valueOf(response.getStatus()));
    } else if (ex instanceof BadRequestException) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
    } else if (ex instanceof NoLoginException) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
    } else if (ex instanceof ResourceNotFoundException || ex instanceof InvitationTokenExpiredException
            || ex instanceof InvitationTokenInvalidException || ex instanceof RegisterTokenInvalidException
            || ex instanceof ResetPasswordTokenInvalidException) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } else {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    mav.setView(new MappingJacksonJsonView());
    mav.addObject("exception", ex);
    logger.debug("view name = {}", mav.getViewName());

    return mav;
}
 
开发者ID:sercxtyf,项目名称:onboard,代码行数:27,代码来源:ExceptionHandler.java

示例2: testJsonOnly

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
@Test
public void testJsonOnly() throws Exception {

	standaloneSetup(new PersonController()).setSingleView(new MappingJacksonJsonView()).build()
		.perform(get("/person/Corea"))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_JSON))
			.andExpect(jsonPath("$.person.name").value("Corea"));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:10,代码来源:ViewResolutionTests.java

示例3: testContentNegotiation

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
@Test
public void testContentNegotiation() throws Exception {

	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<View>();
	viewList.add(new MappingJacksonJsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:40,代码来源:ViewResolutionTests.java

示例4: contentNegotiatingViewResolver

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
开发者ID:v5developer,项目名称:maven-framework-project,代码行数:16,代码来源:WebMvcConfig.java

示例5: addLegalRepresentative

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
/**
 * Adds the legal representative.
 *
 * @param newLegalRepresentativeDto the new legal representative dto
 * @param result the result
 * @param response the response
 * @param model the model
 * @return the model and view
 * @throws ParseException the parse exception
 */
@RequestMapping(value="connectionMain.html", method=RequestMethod.POST)
public ModelAndView addLegalRepresentative(@Valid LegalRepresentativeDto newLegalRepresentativeDto,
		BindingResult result,
		HttpServletResponse response,
		Model model) throws ParseException {
	
	fieldValidator.validate(newLegalRepresentativeDto, result);
	if (result.hasErrors()) {
		MappingJacksonJsonView view = new MappingJacksonJsonView();
		Map<String, String> map = new HashMap<String, String>();
		
		List<FieldError> errors = result.getFieldErrors();
		for (FieldError error : errors) {
			for(String errorCode : error.getCodes()) {
				try {
					String errorMessage = messageProperties.getMessage(errorCode, null, null);
					String[] a = errorCode.split("\\.");
					
					//the third token is the field name
					map.put(a[a.length - 1], errorMessage);
				} catch(NoSuchMessageException e) {
					continue;
				}
			}
		}
		
		view.setAttributesMap(map);
		ModelAndView mav = new ModelAndView();
		mav.setView(view);
		
		//return json if validation not pass
		return mav;
	}
	
	AuthenticatedUser currentUser = userContext.getCurrentUser();
	
	PatientProfileDto legalRepDto = patientLegalRepresentativeAssociationService.getPatientDtoFromLegalRepresentativeDto(newLegalRepresentativeDto);
	PatientLegalRepresentativeAssociationDto associationDto = patientLegalRepresentativeAssociationService.getAssociationDtoFromLegalRepresentativeDto(newLegalRepresentativeDto);
	
	//save to get id
	PatientProfileDto updatedLegalRepresentativeDto = patientService.savePatient(legalRepDto);
	associationDto.setPatientId(patientService.findPatientProfileByUsername(currentUser.getUsername()).getId());
	associationDto.setLegalRepresentativeId(updatedLegalRepresentativeDto.getId());
	patientLegalRepresentativeAssociationService.savePatientLegalRepresentativeAssociationDto(associationDto);
	
	//return html if validation passed
	return new ModelAndView("redirect:/patients/connectionMain.html");
}
 
开发者ID:tlin-fei,项目名称:ds4p,代码行数:59,代码来源:LegalRepresentativeController.java

示例6: asModelAndView

import org.springframework.web.servlet.view.json.MappingJacksonJsonView; //导入依赖的package包/类
public ModelAndView asModelAndView() {
        MappingJacksonJsonView jsonView = new MappingJacksonJsonView();
        return new ModelAndView(jsonView, ImmutableMap.of("error", message));
}
 
开发者ID:Polygon4,项目名称:izzymongo,代码行数:5,代码来源:ControllerException.java


注:本文中的org.springframework.web.servlet.view.json.MappingJacksonJsonView类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。