Search in sources :

Example 6 with TypeMismatchException

use of org.springframework.beans.TypeMismatchException in project spring-framework by spring-projects.

the class MarshallingMessageConverter method convertFromInternal.

@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
    Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
    try {
        Source source = getSource(message.getPayload());
        Object result = this.unmarshaller.unmarshal(source);
        if (!targetClass.isInstance(result)) {
            throw new TypeMismatchException(result, targetClass);
        }
        return result;
    } catch (Exception ex) {
        throw new MessageConversionException(message, "Could not unmarshal XML: " + ex.getMessage(), ex);
    }
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TypeMismatchException(org.springframework.beans.TypeMismatchException)

Example 7 with TypeMismatchException

use of org.springframework.beans.TypeMismatchException in project spring-framework by spring-projects.

the class ResponseStatusExceptionResolverTests method nestedException.

// SPR-12903
@Test
public void nestedException() throws Exception {
    Exception cause = new StatusCodeAndReasonMessageException();
    TypeMismatchException ex = new TypeMismatchException("value", ITestBean.class, cause);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertResolved(mav, 410, null);
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) ModelAndView(org.springframework.web.servlet.ModelAndView) ResponseStatusException(org.springframework.web.server.ResponseStatusException) TypeMismatchException(org.springframework.beans.TypeMismatchException) Test(org.junit.Test)

Example 8 with TypeMismatchException

use of org.springframework.beans.TypeMismatchException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method typeMismatch.

@Test
public void typeMismatch() {
    Exception ex = new TypeMismatchException("foo", String.class);
    testException(ex);
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.Test)

Example 9 with TypeMismatchException

use of org.springframework.beans.TypeMismatchException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleTypeMismatch.

@Test
public void handleTypeMismatch() {
    TypeMismatchException ex = new TypeMismatchException("foo", String.class);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertNotNull("No ModelAndView returned", mav);
    assertTrue("No Empty ModelAndView returned", mav.isEmpty());
    assertEquals("Invalid status code", 400, response.getStatus());
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.Test)

Example 10 with TypeMismatchException

use of org.springframework.beans.TypeMismatchException in project gocd by gocd.

the class GoConfigFieldLoader method setValue.

private void setValue(Object val) {
    try {
        ConfigAttributeValue configAttributeValue = field.getType().getAnnotation(ConfigAttributeValue.class);
        if (configAttributeValue != null) {
            if (val != null || configAttributeValue.createForNull()) {
                Constructor<?> constructor = field.getType().getConstructor(String.class);
                field.set(instance, constructor.newInstance(new Object[] { val }));
            }
        } else if (val != null) {
            Object convertedValue = typeConverter.convertIfNecessary(val, field.getType());
            field.set(instance, convertedValue);
        }
    } catch (IllegalAccessException e) {
        throw bomb("Error setting configField: " + field.getName(), e);
    } catch (TypeMismatchException e) {
        final String message = format("Could not set value [{0}] on Field [{1}] of type [{2}] ", val, field.getName(), field.getType());
        throw bomb(message, e);
    } catch (NoSuchMethodException e) {
        throw bomb("Error setting configField: " + field.getName() + " as " + field.getType(), e);
    } catch (InstantiationException e) {
        throw bomb("Error creating configAttribute: " + field.getName() + " as " + field.getType(), e);
    } catch (InvocationTargetException e) {
        throw bomb("Error creating configAttribute: " + field.getName() + " as " + field.getType(), e);
    }
}
Also used : TypeMismatchException(org.springframework.beans.TypeMismatchException) ConfigAttributeValue(com.thoughtworks.go.config.ConfigAttributeValue) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

TypeMismatchException (org.springframework.beans.TypeMismatchException)12 Test (org.junit.Test)3 TypeConverter (org.springframework.beans.TypeConverter)3 MethodParameter (org.springframework.core.MethodParameter)3 HashSet (java.util.HashSet)2 BeansException (org.springframework.beans.BeansException)2 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)2 InjectionPoint (org.springframework.beans.factory.InjectionPoint)2 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)2 BindException (org.springframework.validation.BindException)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1 DocumentNotFoundException (com.baidu.dsp.common.exception.DocumentNotFoundException)1 FieldException (com.baidu.dsp.common.exception.FieldException)1 GlobalExceptionAware (com.baidu.dsp.common.exception.base.GlobalExceptionAware)1 ConfigAttributeValue (com.thoughtworks.go.config.ConfigAttributeValue)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1