Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:07:37+00:00 2026-06-14T05:07:37+00:00

Spring 3 has such a nice feature as type conversion. It provides a converter

  • 0

Spring 3 has such a nice feature as type conversion. It provides a converter SPI(Converter<S, T>) to be used to implement differenet conversion logic.
The subclass of Converter type allow to define one-way conversion(only from S to T), so if I want a conversion also to be performed from T to S I need to define another converter class that implement Converter<T, S>. If I have many classes which are subject to conversion, i need to define many converters.
Is there any posibility to define two-way conversion logic(from S to T and from T to S) in one converter? and how it will be used?

PS. now I’m using my converters via ConversionServiceFactoryBean defining/injecting them in configuration file

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:07:38+00:00Added an answer on June 14, 2026 at 5:07 am

    You are correct, if you want to use the org.springframework.core.convert.converter.Converter interface directly, you’ll need to implement two converters, one for each direction.

    But spring 3 has a couple of other options:

    1. If your conversion is not object-to-object but rather object-to-string (and back), then you can implement a org.springframework.format.Formatter instead. Formatters get registered as GenericConverters (see http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch05s07.html#converter-upgrade-to-spring-3)

    2. Otherwise you could implement your own org.springframework.core.convert.converter.GenericConverter, which makes it easy to create TwoWayConverter implementations using reflection.

      public abstract class AbstractTwoWayConverter<S, T> implements GenericConverter {
      
          private Class<S> classOfS;
          private Class<T> classOfT;
      
          protected AbstractTwoWayConverter() {
              Type typeA = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
              Type typeB = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
              this.classOfS = (Class) typeA;
              this.classOfT = (Class) typeB;
          }
      
          public Set<ConvertiblePair> getConvertibleTypes() {
              Set<ConvertiblePair> convertiblePairs = new HashSet<ConvertiblePair>();
              convertiblePairs.add(new ConvertiblePair(classOfS, classOfT));
              convertiblePairs.add(new ConvertiblePair(classOfT, classOfS));
              return convertiblePairs;
          }
      
          public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
              if (classOfS.equals(sourceType.getType())) {
                  return this.convert((S) source);
              } else {
                  return this.convertBack((T) source);
              }
          }
      
          protected abstract T convert(S source);
      
          protected abstract S convertBack(T target);
      
      }
      
      /** 
       * converter to convert between a userId and user.
       * this class can be registered like so: 
       * conversionService.addConverter(new UserIdConverter (userDao));
       */ 
      public class UserIdConverter extends AbstractTwoWayConverter<String, User> {
      
          private final UserDao userDao;
      
          @Autowired
          public UserIdConverter(UserDao userDao) {
              this.userDao = userDao;
          }
      
          @Override
          protected User convert(String userId) {
              return userDao.load(userId);
          }
      
          @Override
          protected String convertBack(User target) {
              return target.getUserId();
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Spring has the nice mechanism PropertyPlaceholderConfigurer for injecting values like timeouts, JDBC Urls and
Say a web page has a string such as I am a simple string
I have a property such as IdentityCode string IdentityCode {get;set;} my IdentityCode has three
I see that Spring has a @Required annotation to mark member variables in beans
I am working on a project that has Spring based Web Services and Spring
Does anybody has an experience with Spring Integration project as embedded ESB? I'm highly
I have a Spring Batch process which has following kind of code. <step id=step1
I have a Spring application that I believe has some bottlenecks, so I'd like
I'm fairly new to Spring and I need a bean that has two properties
I am using a custom MVC application that has dependencies on SPring Batch as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.