3

I have a very simple Spring Boot MVC project in which I try to use the DomainClassConverter to load the Entity directly. But it seems that the DomainClassConverter is not found. I have the following error when accessing to accessing to the 'localhost:8080/one/2' url:

Cannot convert value of type 'java.lang.String' to required type 'com.example.test.data.Customer': no matching editors or conversion strategy found

But The DomainClassConverter should be enabled by Spring Boot and manage the conversion.

I tryed also to enable it explicitely via the @EnableSpringDataWebSupport annotation but it didn't work neither.

Here is my controller code:

@Controller
public class TestController {

    @Autowired
    private CustomerRepository customerRepository;

    @GetMapping("/all")
    public void all(Model model) {
        Iterable<Customer> customers=customerRepository.findAll();
        model.addAttribute("customers",customers);
    };

    @GetMapping("/one/{customer_id}")
    public void one(@PathVariable("customer_id") Customer customer, Model model) {
        model.addAttribute("customer",customer);
    };
}

The Customer coode:

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Getter
    private Long id;

    @Getter
    @Setter
    private String firstName;

    @Getter
    @Setter
    private String lastName;

    protected Customer() {
    }

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

}

The CustomerREpositoy:

public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);

    Customer findById(long id);
}

The application :

@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class);
    }
}

And finally the build.graddle:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
//    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.postgresql:postgresql'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
//    testImplementation 'org.springframework.security:spring-security-test'
}

test {
    useJUnitPlatform()
}

Any idea?

Javaddict
  • 483
  • 1
  • 4
  • 15
  • Does this answer your question? [Issues using Spring's DomainClassConverter in Spring MVC](https://stackoverflow.com/questions/20695180/issues-using-springs-domainclassconverter-in-spring-mvc) – Lakshan Jul 11 '20 at 05:45
  • This appears to be a known bug in spring data commons 2.3, see https://jira.spring.io/browse/DATACMNS-1759. I'm having the same problem in my project and until that bug is fixed I replaced org.springframework.data.repository.support.DomainClassConverter with the version from 2.2.x and so far it seems to be working for me. – Lukas Hinsch Jul 17 '20 at 08:27

1 Answers1

0

you try to get the customer_id (path variable) as a Customer object. hence, get the above error when you try to access localhost:8080/one/2.

change the customer_id (path variable) data type to relevant data type (String, int etc.) as follows,

@GetMapping("/one/{customer_id}")
public void one(@PathVariable("customer_id") String customerId, Model model) {
    ----
};
Lakshan
  • 1,404
  • 3
  • 10
  • 23
  • I know that It would work like that but I want the DomainClassConverter to convert the customer_id to the corresponding Customer instance. This would avoid a lot of plumbey . The problem is that it doesn't work. – Javaddict Jul 10 '20 at 18:47