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 7929083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:57:24+00:00 2026-06-03T19:57:24+00:00

I have such enum: public enum PartnershipIndicator { VENDOR(VENDOR), COPARTNER(COPARTNER), BUYER(BUYER); String code; private

  • 0

I have such enum:

public enum PartnershipIndicator {
    VENDOR("VENDOR"), COPARTNER("COPARTNER"), BUYER("BUYER");

    String code;

    private PartnershipIndicator(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public static PartnershipIndicator valueOfCode(String code) {
        for (PartnershipIndicator status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException(
            "Partnership status cannot be resolved for code " + code);
    }

    @Override
    public String toString() {
        return code;
    }
}

I need to convert it to String and vice versa. Now, it is done by custom converter. But i want to do it via dozer mappings (if it is possible). If i do not write any mappings to the dozer confing, i get

org.dozer.MappingException: java.lang.NoSuchMethodException: by.dev.madhead.demo.test_java.model.PartnershipIndicator.<init>()

exception. I cannot add default public constructor to enum, as it is not possible. So, i wrote a trick with internal code and valueOfCode() / toString(). It does not work. Then, i’ve mapped it in dozer config:

<mapping>
    <class-a>java.lang.String</class-a>
    <class-b create-method="valueOfCode">by.dev.madhead.demo.test_java.model.PartnershipIndicator</class-b>
</mapping>

It does not work. I tried valueOfCode(), one-way mappings. Nothing works. Enum to String conversion does not work too, i get empty Strings.
Any ideas?

  • 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-03T19:57:25+00:00Added an answer on June 3, 2026 at 7:57 pm

    Not sure if this is still an issue, but maybe help for anyone searching. But here is implemented solution to this:

    @Override
    public Object convert(Object destination, Object source, Class<?> destinationClass,    Class<?> sourceClass) {
        if(source == null)
            return null;
        if(destinationClass != null){
            if(destinationClass.getSimpleName().equalsIgnoreCase("String")){
                return this.getString(source);
            }else if( destinationClass.isEnum()){
    
                return this.getEnum(destinationClass, source);
    
            }else{
                throw new MappingException(new StrBuilder("Converter ").append(this.getClass().getSimpleName())
                           .append(" was used incorrectly. Arguments were: ")
                           .append(destinationClass.getClass().getName())
                           .append(" and ")
                           .append(source).toString());
            }
        }
        return null;
    }
    
    private Object getString(Object object){
        String value = object.toString();
        return value;
    }
    private Object getEnum(Class<?> destinationClass, Object source){
        Object enumeration = null;
    
        Method [] ms = destinationClass.getMethods();
        for(Method m : ms){
            if(m.getName().equalsIgnoreCase("valueOf")){
                try {
                    enumeration = m.invoke( destinationClass.getClass(), (String)source);
                }
                catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                return enumeration;
            }
        }
        return null;
    }
    

    The StrBuilder class when building the exception message is from the apaches common-lang libs. But other than that a simple reflection to solve this issue. Just add to a class that implements CustomConverter and then in your dozer mapping xml file add the following configuration:

    <configuration>
        <custom-converters>
            <converter type="com.yourcompany.manager.utils.dozer.converters.EnumStringBiDirectionalDozerConverter">
                <class-a>java.lang.Enum</class-a>
                <class-b>java.lang.String</class-b>
            </converter>
        </custom-converters>
    </configuration>
    

    Note that you can only list a configuration once between all of your mapping files (if you have multiple) otherwise dozer will complain. What I typically do is place my custom converter configurations in one file for simplicity. Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such class public unsafe class EigenSolver { public double* aPtr {get; private
I have an Enum called Status defined as such: public enum Status { VALID(valid),
I have such an enum and a property. public enum Type { Hourly =
I have a bunch of different enums, such as... public enum MyEnum { [Description(Army
I have the following enumerator in my code: Public Enum UserSearchFields LastName FirstName Email
In Java, with Sun's JDK 1.6, with an enum such as this: public enum
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> I
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person

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.