title: Spring Data Rest Field converter
body: <p>I am having a hard time using my custom converters on <strong>spring data REST project (controller free application and strictly java configuration).</strong></p>
  
  <p>I have two Entities, an Employee and a State. The relationship is @ManyToOne, I am sure we all know that. Anyway, the problem is converting the <code>state</code> field (field name is state) from <code>String</code> to a <code>State</code> object and <code>setState()</code> in the <code>Employee</code> class for persistence to a database.</p>
  
  <pre><code>package com.hr.domain;
  
  @Entity
  public class Employee implements Serializable {
  
     private static final long serialVersionUID = 1L;
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "state_id", nullable = true)
     private Long id;
     private String firstname;
     private State state;
  
     @StateConverter
     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "employee_state_id_fk", nullable = false, insertable = true, updatable = true)
     private State state;
  
     //GETTERS AND SETTERS
     public String getFirstname() {
        return firstname();
     }
  
     public void setFirstname(String firstname) {
        this.firstname = firstname;
     }
  
     public String getState() {
      return state();
     }
  
     public void setState(State state) {
       this.state = state;
     }
  
     //HASHCODES AND EQUALS
  
  }
  
  
  package com.hr.domain;
  
  @Entity
  public class State implements Serializable {
  
     private static final long serialVersionUID = 1L;
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "state_id", nullable = true)
     private Long id;
     private String state_name;
  
     //GETTERS AND SETTERS
  
    //TO STRING
    @Override
    public String toString() {
      return "State [id=" + id + ", state_name=" + state_name + "]";
    }       
  
  }
  </code></pre>
  
  <p>I have registered my converter with the conversion service but still cant get the String to be converter to State object on form submit.</p>
  
  <pre><code>@Bean//(name="conversionService")
  public ConversionService getConversionService() {       
      ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
      bean.setConverters(getConverters());
      bean.afterPropertiesSet();
      ConversionService object = bean.getObject();
      System.out.println(object);
      return object;
  }
  
  private Set&lt;Converter&gt; getConverters() {    
      Set&lt;Converter&gt; converters = new HashSet&lt;Converter&gt;();
      converters.add(new StringToStateTypeConverter());
      return converters;      
  }
  </code></pre>
  
  <p>This is my StateConverter</p>
  
  <pre><code>@Component
  @StateConverter
  public class StringToStateTypeConverter implements Converter&lt;String, State&gt; {
  
      @Autowired
      StateRepository repository;
  
      public State convert(String source) {
          return repository.findOne(new Long(source));
      }
  
  }
  </code></pre>
  
  <p>On submission, I get this error:</p>
  
  <blockquote>
    <p>Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB';</p>
  </blockquote>
  
  <p>Any form of help will be appreciated.</p>
  
  <p>May the force be with you
  Regards.</p>
  
  <p><strong>Here is the Error trace</strong></p>
  
  <pre><code>17:21:29,975 ERROR [org.springframework.data.rest.webmvc.AbstractRepositoryRestController] (default task-13) Could not read JSON: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]): org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"])
      at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228) [spring-web-4.0.5.RELEASE.jar:4.0.5.RELEASE]
  
  ...........
  Caused by: com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable. (through reference chain: com.hr.domain.Employee["State"])
      at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:232) [jackson-databind-2.3.3.jar:2.3.3]
      at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:197) [jackson-databind-2.3.3.jar:2.3.3]
  
  Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.net.URI to type com.hr.domain.State for value 'AB'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable.
      at org.springframework.data.rest.core.UriToEntityConverter.convert(UriToEntityConverter.java:106) [spring-data-rest-core-2.1.0.RELEASE.jar:]
      at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$UriStringDeserializer.deserialize(PersistentEntityJackson2Module.java:359) [spring-data-rest-webmvc-2.1.0.RELEASE.jar:]
  
  Caused by: java.lang.IllegalArgumentException: Cannot resolve URI AB. Is it local or remote? Only local URIs are resolvable.
      ... 94 more
  </code></pre>