@Test public void testConstructFromSpringErrors() { MessageCodesResolver mockMessageCodesResolver = mock(MessageCodesResolver.class); when(mockMessageCodesResolver.resolveMessageCodes("default.blank.message", "foo", "bar", null)).thenReturn(new String[] {"Property [bar] cannot be blank"}); when(mockMessageCodesResolver.resolveMessageCodes("default.blank.message", "foo", "baz", null)).thenReturn(new String[] {"Property [baz] cannot be blank"}); MapBindingResult errors = new MapBindingResult(new HashMap(), "foo"); errors.setMessageCodesResolver(mockMessageCodesResolver); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bar", "default.blank.message"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "baz", "default.blank.message"); JsonCError jsonc = new JsonCError(errors); assertThat(jsonc.getCode(), is("Property [bar] cannot be blank")); assertThat(jsonc.getMessage(), is("Field error in object 'foo' on field 'bar': rejected value [null]; codes [Property [bar] cannot be blank]; arguments []; default message [null]")); List<Map<String,Object>> errorList = jsonc.getErrors(); assertThat((String) errorList.get(0).get("code"), is("Property [bar] cannot be blank")); assertThat((String) errorList.get(0).get("message"), is("Field error in object 'foo' on field 'bar': rejected value [null]; codes [Property [bar] cannot be blank]; arguments []; default message [null]")); assertThat((String) errorList.get(1).get("code"), is("Property [baz] cannot be blank")); assertThat((String) errorList.get(1).get("message"), is("Field error in object 'foo' on field 'baz': rejected value [null]; codes [Property [baz] cannot be blank]; arguments []; default message [null]")); }
public MessageCodesResolver getMessageCodesResolver() { List<MessageCodesResolver> candidates = new ArrayList<MessageCodesResolver>(); for (WebMvcConfigurer configurer : this.delegates) { MessageCodesResolver messageCodesResolver = configurer.getMessageCodesResolver(); if (messageCodesResolver != null) { candidates.add(messageCodesResolver); } } return selectSingleInstance(candidates, MessageCodesResolver.class); }
@Override public MessageCodesResolver getMessageCodesResolver() { List<MessageCodesResolver> candidates = new ArrayList<MessageCodesResolver>(); for (WebMvcConfigurer configurer : this.delegates) { MessageCodesResolver messageCodesResolver = configurer.getMessageCodesResolver(); if (messageCodesResolver != null) { candidates.add(messageCodesResolver); } } return selectSingleInstance(candidates, MessageCodesResolver.class); }
@SuppressWarnings("serial") @Override public MessageCodesResolver getMessageCodesResolver() { return new DefaultMessageCodesResolver() { @Override public String[] resolveMessageCodes(String errorCode, String objectName) { return new String[] { "custom." + errorCode }; } }; }
@Test public void testMessageCodesResolver() { loadBeanDefinitions("mvc-config-message-codes-resolver.xml"); RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class); assertNotNull(adapter); Object initializer = adapter.getWebBindingInitializer(); assertNotNull(initializer); MessageCodesResolver resolver = ((ConfigurableWebBindingInitializer) initializer).getMessageCodesResolver(); assertNotNull(resolver); assertEquals(TestMessageCodesResolver.class, resolver.getClass()); assertEquals(false, new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")); }
@Override public MessageCodesResolver getMessageCodesResolver() { if (this.mvcProperties.getMessageCodesResolverFormat() != null) { DefaultMessageCodesResolver resolver = new DefaultMessageCodesResolver(); resolver.setMessageCodeFormatter( this.mvcProperties.getMessageCodesResolverFormat()); return resolver; } return null; }
/** * Return the strategy to use for resolving errors into message codes. */ public final MessageCodesResolver getMessageCodesResolver() { return this.messageCodesResolver; }
@Override public MessageCodesResolver getMessageCodesResolver() { return null; }
/** * Override this method to provide a custom {@link MessageCodesResolver}. */ protected MessageCodesResolver getMessageCodesResolver() { return null; }
/** * {@inheritDoc} * <p>This implementation is empty. */ @Override public MessageCodesResolver getMessageCodesResolver() { return null; }
@Override protected MessageCodesResolver getMessageCodesResolver() { return this.configurers.getMessageCodesResolver(); }
/** * Return the strategy to use for resolving errors into message codes (if any). */ public final MessageCodesResolver getMessageCodesResolver() { return this.messageCodesResolver; }
/** * Set the strategy to use for resolving errors into message codes. * Applies the given strategy to all data binders used by this controller. * <p>Default is {@code null}, i.e. using the default strategy of * the data binder. * @see org.springframework.validation.DataBinder#setMessageCodesResolver */ public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { this.messageCodesResolver = messageCodesResolver; }
/** * Provide a custom {@link MessageCodesResolver} for building message codes * from data binding and validation error codes. Leave the return value as * {@code null} to keep the default. */ MessageCodesResolver getMessageCodesResolver();
/** * Set the strategy to use for resolving errors into message codes. * Applies the given strategy to all data binders used by this controller. * <p>Default is {@code null}, i.e. using the default strategy of * the data binder. * @see #createBinder * @see org.springframework.validation.DataBinder#setMessageCodesResolver */ public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { this.messageCodesResolver = messageCodesResolver; }
/** * Set the strategy to use for resolving errors into message codes. * Applies the given strategy to all data binders used by this controller. * <p>Default is {@code null}, i.e. using the default strategy of the data binder. * @see #createBinder * @see org.springframework.validation.DataBinder#setMessageCodesResolver */ public final void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) { this.messageCodesResolver = messageCodesResolver; }
/** * {@inheritDoc} * <p>This implementation is empty. */ public MessageCodesResolver getMessageCodesResolver() { return null; }