Search in sources :

Example 6 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project cas by apereo.

the class Cas30JsonResponseView method createDelegatedView.

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;
}
Also used : MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView)

Example 7 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project spring-security-oauth by spring-projects.

the class WebMvcConfig method contentViewResolver.

@Bean
public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setDefaultViews(Arrays.<View>asList(new MappingJackson2JsonView()));
    return contentViewResolver;
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) Bean(org.springframework.context.annotation.Bean) ConversionServiceFactoryBean(org.springframework.context.support.ConversionServiceFactoryBean)

Example 8 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project spring-security-oauth by spring-projects.

the class WebMvcConfig method contentViewResolver.

@Bean
public ContentNegotiatingViewResolver contentViewResolver() throws Exception {
    ContentNegotiationManagerFactoryBean contentNegotiationManager = new ContentNegotiationManagerFactoryBean();
    contentNegotiationManager.addMediaType("json", MediaType.APPLICATION_JSON);
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    MappingJackson2JsonView defaultView = new MappingJackson2JsonView();
    defaultView.setExtractValueFromSingleKeyModel(true);
    ContentNegotiatingViewResolver contentViewResolver = new ContentNegotiatingViewResolver();
    contentViewResolver.setContentNegotiationManager(contentNegotiationManager.getObject());
    contentViewResolver.setViewResolvers(Arrays.<ViewResolver>asList(viewResolver));
    contentViewResolver.setDefaultViews(Arrays.<View>asList(defaultView));
    return contentViewResolver;
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 9 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project Corgi by kevinYin.

the class HandlerMethodExceptionResolver method handleResponseBody.

/**
 * 处理异常(ResponseBody)
 *
 * @param request
 * @param response
 * @param exception
 * @return
 */
private ModelAndView handleResponseBody(HttpServletRequest request, HttpServletResponse response, Exception exception) {
    if (exception instanceof IllegalArgumentException) {
        String uri = getURI(request);
        if (uri.contains("/open/monitor")) {
            throw (IllegalArgumentException) exception;
        }
    }
    String message = getThrowableMessage(exception);
    Map<String, Object> attributes = new HashMap<>(4);
    int code = getErrorCode();
    if (StringUtils.isNotEmpty(errorCodeAttr)) {
        attributes.put("code", Integer.valueOf(code));
    }
    if (StringUtils.isNotEmpty(errorMessageAttr)) {
        attributes.put("message", message);
    }
    MappingJackson2JsonView view = new MappingJackson2JsonView();
    view.setAttributesMap(attributes);
    ModelAndView model = new ModelAndView();
    model.setView(view);
    return model;
}
Also used : HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView)

Example 10 with MappingJackson2JsonView

use of org.springframework.web.servlet.view.json.MappingJackson2JsonView in project cas by apereo.

the class OAuth20RevocationEndpointController method generateRevocationResponse.

/**
 * Generate revocation token response.
 *
 * @param token    the token to revoke
 * @param clientId the client who requests the revocation
 * @param response the response
 * @return the model and view
 * @throws Exception the exception
 */
protected ModelAndView generateRevocationResponse(final String token, final String clientId, final HttpServletResponse response) throws Exception {
    val registryToken = getConfigurationContext().getTicketRegistry().getTicket(token, OAuth20Token.class);
    if (registryToken == null) {
        LOGGER.error("Provided token [{}] has not been found in the ticket registry", token);
    } else if (isRefreshToken(registryToken) || isAccessToken(registryToken)) {
        if (!StringUtils.equals(clientId, registryToken.getClientId())) {
            LOGGER.warn("Provided token [{}] has not been issued for the service [{}]", token, clientId);
            return OAuth20Utils.writeError(response, OAuth20Constants.INVALID_REQUEST);
        }
        if (isRefreshToken(registryToken)) {
            revokeToken((OAuth20RefreshToken) registryToken);
        } else {
            revokeToken(registryToken.getId());
        }
    } else {
        LOGGER.error("Provided token [{}] is either not a refresh token or not an access token", token);
        return OAuth20Utils.writeError(response, OAuth20Constants.INVALID_REQUEST);
    }
    val mv = new ModelAndView(new MappingJackson2JsonView());
    mv.setStatus(HttpStatus.OK);
    return mv;
}
Also used : lombok.val(lombok.val) ModelAndView(org.springframework.web.servlet.ModelAndView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) OAuth20RefreshToken(org.apereo.cas.ticket.refreshtoken.OAuth20RefreshToken)

Aggregations

MappingJackson2JsonView (org.springframework.web.servlet.view.json.MappingJackson2JsonView)22 ModelAndView (org.springframework.web.servlet.ModelAndView)11 ContentNegotiatingViewResolver (org.springframework.web.servlet.view.ContentNegotiatingViewResolver)7 lombok.val (lombok.val)5 Test (org.junit.jupiter.api.Test)5 MarshallingView (org.springframework.web.servlet.view.xml.MarshallingView)5 ArrayList (java.util.ArrayList)4 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)4 View (org.springframework.web.servlet.View)4 InternalResourceViewResolver (org.springframework.web.servlet.view.InternalResourceViewResolver)4 ValidationError (com.breuninger.arch.playground.common.domain.ValidationError)3 HashMap (java.util.HashMap)3 ContentNegotiationManager (org.springframework.web.accept.ContentNegotiationManager)3 FixedContentNegotiationStrategy (org.springframework.web.accept.FixedContentNegotiationStrategy)3 HeaderContentNegotiationStrategy (org.springframework.web.accept.HeaderContentNegotiationStrategy)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 LinkedHashMap (java.util.LinkedHashMap)2 Bean (org.springframework.context.annotation.Bean)2 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)2