Class MessageBuilder

java.lang.Object
org.springframework.binding.message.MessageBuilder

public class MessageBuilder extends Object
A convenient builder for building MessageResolver objects programmatically. Often used by model code such as validation logic to conveniently record validation messages. Supports the production of message resolvers that hard-code their message text, as well as message resolvers that retrieve their text from a message resource bundle. Usage example:

new MessageBuilder().error().source("field").code("mycode").arg(arg1).arg(arg2).defaultText("text").build();

Author:
Keith Donald, Jeremy Grelle
  • Constructor Details

    • MessageBuilder

      public MessageBuilder()
  • Method Details

    • info

      public MessageBuilder info()
      Records that the message being built is an informational message.
      Returns:
      this, for fluent API usage
    • warning

      public MessageBuilder warning()
      Records that the message being built is a warning message.
      Returns:
      this, for fluent API usage
    • error

      public MessageBuilder error()
      Records that the message being built is an error message.
      Returns:
      this, for fluent API usage
    • fatal

      public MessageBuilder fatal()
      Records that the message being built is a fatal message.
      Returns:
      this, for fluent API usage
    • source

      public MessageBuilder source(Object source)
      Records that the message being built is against the provided source.
      Parameters:
      source - the source to associate the message with
      Returns:
      this, for fluent API usage
    • code

      public MessageBuilder code(String code)
      Records that the message being built should try and resolve its text using the code provided. Adds the code to the codes list. Successive calls to this method add additional codes. Codes are applied in the order they are added.
      Parameters:
      code - the message code
      Returns:
      this, for fluent API usage
    • codes

      public MessageBuilder codes(String... codes)
      Records that the message being built should try and resolve its text using the codes provided. Adds the codes to the codes list. Successive calls to this method add additional codes. Codes are applied in the order they are added.
      Parameters:
      codes - the message codes; if null, no changes will be made
      Returns:
      this, for fluent API usage
    • arg

      public MessageBuilder arg(Object arg)
      Records that the message being built has a variable argument. Adds the arg to the args list. Successive calls to this method add additional args. Args are applied in the order they are added.
      Parameters:
      arg - the message argument value
      Returns:
      this, for fluent API usage
    • args

      public MessageBuilder args(Object... args)
      Records that the message being built has variable arguments. Adds the args to the args list. Successive calls to this method add additional args. Args are applied in the order they are added.
      Parameters:
      args - the message argument values, if null no changes will be made
      Returns:
      this, for fluent API usage
    • resolvableArg

      public MessageBuilder resolvableArg(Object arg)
      Records that the message being built has a variable argument, whose display value is also MessageSourceResolvable. Adds the arg to the args list. Successive calls to this method add additional resolvable args. Args are applied in the order they are added.
      Parameters:
      arg - the resolvable message argument
      Returns:
      this, for fluent API usage
    • resolvableArgs

      public MessageBuilder resolvableArgs(Object... args)
      Records that the message being built has variable arguments, whose display values are also MessageSourceResolvable instances. Adds the args to the args list. Successive calls to this method add additional resolvable args. Args are applied in the order they are added.
      Parameters:
      args - the resolvable message arguments
      Returns:
      this, for fluent API usage
    • defaultText

      public MessageBuilder defaultText(String text)
      Records the fallback text of the message being built. If the message has no codes, this will always be used as the text. If the message has codes but none can be resolved, this will always be used as the text.
      Parameters:
      text - the default text
      Returns:
      this, for fluent API usage
    • build

      public MessageResolver build()
      Builds the message that will be resolved. Called after the end of recording builder instructions.
      Returns:
      the built message resolver