4

I'm trying to implement a Springboot REST api. and the class where I have defined the @RestController doesn't seem to be working. I have a class called MyService where it implements all the abstract methods. I have added the @RestController annotation on top of the class declaration and added the @RequestMapping annotation for the method that I need to call from the rest call. But this doesn't work. I tried this with a class which does not implement any interface and that works fine.

Here is the code

package com.my.service;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyService implements MyServiceInterface{

    @Override
    @RequestMapping("/age")
    public String Age() {
        return  "24";
    }
}

the code of MyServiceInterface

public interface MyServiceInterface {
    public String Age();
}

And the error I'm getting from postman is

{"timestamp":1489688505136,"status":404,"error":"Not Found","message":"No message available","path":"/age"}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Madushika Perera
  • 402
  • 2
  • 5
  • 13
  • Could you please share your "MyServiceInterface" and share the stacktrace of the exception you are getting.... – VelNaga Mar 16 '17 at 17:13
  • @VelNaga check my edit – Madushika Perera Mar 16 '17 at 18:23
  • When you start spring-boot application, it prints all the mappings in the console and/or log. See if `/age` was mapped to anything. Your problem could be in configuration. Also in Java methods names start with lower case letters. – jny Mar 16 '17 at 21:01
  • @jny I checked the console and /age is not mapped others are mapped (the one where their classes are not implementing any interface) – Madushika Perera Mar 17 '17 at 04:06
  • If "/age" is not mapped means check your package structure .....The package should be a sub-package of your main class or configure @ComponentScan with your package in base package....Simply share your package structure... – VelNaga Mar 17 '17 at 09:18

2 Answers2

2

I just had the same problem during contract testing. I was very confused because there were some other @RestControllers that implemented their interface and they were correctly mapped.

Then I started my application as in prod mode and realized that the controller was correctly mapped. Extremely strange!

I was about to delete the interface and suddenly I thought of Spring proxying. Don't know why it came to my mind. Then I checked my src/test/application.properties and realized it had this property spring.aop.proxy-target-class=false. I removed it and everything is now working as expected.

Now it all makes sense to me. src/main/application.properties didn't have that property. So Spring proxying method was the default one when on prod mode. On the other hand testing was using the other Spring proxying method. I'm going to check with my teammates why it was there. I believe default Spring proxying method should be the one to go with.

Gus Vargas
  • 31
  • 4
-3

Found it! it seems like adding @Override is the reason i managed it to get it working by adding @RequestMapping("/age") for a non overriding method. like

@RequestMapping("/age")
public String Age() {
    return  "24";
}
Madushika Perera
  • 402
  • 2
  • 5
  • 13
  • 1
    [at]Override is not the issue here, as my application works just fine. I have an interface and it is implemented in a class and all the methods are annotated with [at]Override as well as request mapping. They work just fine. Please check the real cause. – Jayesh Jan 30 '18 at 16:05