Example usage for org.springframework.http.converter HttpMessageConverter getClass

List of usage examples for org.springframework.http.converter HttpMessageConverter getClass

Introduction

In this page you can find the example usage for org.springframework.http.converter HttpMessageConverter getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.springsource.html5expense.security.EndpointTokenServices.java

public EndpointTokenServices(String oauthAuthenticationUrl) {
    this.oauthAuthenticationUrl = oauthAuthenticationUrl;

    this.restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
    for (HttpMessageConverter<?> httpMessageConverter : messageConverters) {
        if (httpMessageConverter.getClass().equals(MappingJacksonHttpMessageConverter.class)) {
            MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) httpMessageConverter;
            ObjectMapper mapper = new ObjectMapper();
            mapper.getDeserializationConfig().addMixInAnnotations(OAuth2Authentication.class,
                    OAuth2AuthenticationMixin.class);
            jsonConverter.setObjectMapper(mapper);
        }//www. j a  v  a2  s  .co  m
    }
}

From source file:se.sawano.scala.examples.scalaspringmvc.Config.java

private HashMap<String, HttpMessageConverter<?>> getDefaultMessageConverters() {
    RestTemplate dummyTemplate = new RestTemplate();
    HashMap<String, HttpMessageConverter<?>> defaultConverters = new HashMap<String, HttpMessageConverter<?>>();
    for (HttpMessageConverter<?> c : dummyTemplate.getMessageConverters()) {
        defaultConverters.put(c.getClass().toString(), c);
    }//from w w  w.j  av  a 2 s.  c  o m
    return defaultConverters;
}

From source file:com.springsource.samples.resttemplate.ClientRest.java

public void testaggiungiCommento() {

    //      MyMessageConverter<CommentDTO> myMessageConverter = new MyMessageConverter<CommentDTO>();
    //      /*from w  w  w  .  j ava  2  s. c o m*/
    //      List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    //      messageConverters.add(myMessageConverter);
    //      StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
    //      messageConverters.add(stringHttpMessageConverter);
    //      restTemplate.setMessageConverters(messageConverters);

    List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

    for (HttpMessageConverter<?> httpMessageConverter : messageConverters) {

        System.out.println(httpMessageConverter.getClass());
    }

    restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

    CommentDTO commentDTO = new CommentDTO();
    commentDTO.setText("aaa");
    commentDTO.setValore(4);
    //      CommentDTO postForObject = restTemplate.postForObject("http://localhost:8080/rest-validation/api/aggiungiCommento",
    //            commentDTO, CommentDTO.class);

    URI postForLocation = restTemplate
            .postForLocation("http://localhost:8080/rest-validation/api/aggiungiCommento", commentDTO);
    //            
    //      System.out.println(postForLocation.getQuery());
}

From source file:org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.java

/**
 * Create the method argument value of the expected parameter type by reading
 * from the given HttpInputMessage.//from  w  ww  .j a va 2  s . com
 * @param <T> the expected type of the argument value to be created
 * @param inputMessage the HTTP input message representing the current request
 * @param parameter the method parameter descriptor
 * @param targetType the target type, not necessarily the same as the method
 * parameter type, e.g. for {@code HttpEntity<String>}.
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@SuppressWarnings("unchecked")
@Nullable
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
        Type targetType)
        throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {

    MediaType contentType;
    boolean noContentType = false;
    try {
        contentType = inputMessage.getHeaders().getContentType();
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotSupportedException(ex.getMessage());
    }
    if (contentType == null) {
        noContentType = true;
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }

    Class<?> contextClass = parameter.getContainingClass();
    Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
    if (targetClass == null) {
        ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
        targetClass = (Class<T>) resolvableType.resolve();
    }

    HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod()
            : null);
    Object body = NO_VALUE;

    EmptyBodyCheckingHttpInputMessage message;
    try {
        message = new EmptyBodyCheckingHttpInputMessage(inputMessage);

        for (HttpMessageConverter<?> converter : this.messageConverters) {
            Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter
                    .getClass();
            GenericHttpMessageConverter<?> genericConverter = (converter instanceof GenericHttpMessageConverter
                    ? (GenericHttpMessageConverter<?>) converter
                    : null);
            if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType)
                    : (targetClass != null && converter.canRead(targetClass, contentType))) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
                }
                if (message.hasBody()) {
                    HttpInputMessage msgToUse = getAdvice().beforeBodyRead(message, parameter, targetType,
                            converterType);
                    body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse)
                            : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse));
                    body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
                } else {
                    body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);
                }
                break;
            }
        }
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
    }

    if (body == NO_VALUE) {
        if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod)
                || (noContentType && !message.hasBody())) {
            return null;
        }
        throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
    }

    return body;
}