Search code examples
javaspringspring-bootapirest

Why am I not able to access the REST API exposed by my service?


I am trying to incorporate a few APIs into my existing spring-boot service, but I am not able to reach the APIs for some reason, when I try to access it from Postman.

Initially I thought it might be because of missing dependencies, so I added the spring-boot-started-web dependency to my build.gradle

classpath(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.8.RELEASE')

My next troubleshooting steps involved checking the annotations of my app. After checking them, I could not find any issues.

This is the annotations of my config class:

@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, RibbonAutoConfiguration.class})
@EnableScheduling
@ComponentScan({"com.example"})
@SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })
public class MyApplication {
      // Some code
}

and this is my Controller class:

@RestController
public class MyApplication {

       @RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE)
       public ResponseEntity<String> myApiHandler() {
           return new ResponseEntity<String>("Successfully called API", HttpStatus.OK);
       }
}

I had also defined the port in the application.properties file as follows:

server.port=6069

When I try to hit the following API from Postman, however, I am not able to get any response from the application:

localhost:6069/myApi

this is the error I get:

Error: connect ECONNREFUSED 127.0.0.1:6069

I am using Java 8, spring boot version 1.5.8 and Intellij IDE for development. Could you please let me know what I am missing? Any help is appreciated, thanks!


Solution

  • Use

    @GetMapping("/myApi")
    

    instead of

    @RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE)
    

    This must resolve your issue.