英文:
Why am I not able to access the REST API exposed by my service?
问题
我尝试将一些API集成到我的现有Spring Boot服务中,但是当我尝试从Postman访问它时,我无法到达API。最初我认为可能是因为缺少依赖项,所以我将spring-boot-starter-web依赖项添加到我的build.gradle中。
classpath(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.8.RELEASE')
我的下一个故障排除步骤涉及检查我的应用程序的注释。检查后,我没有找到任何问题。
这是我的配置类的注释:
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, RibbonAutoConfiguration.class})
@EnableScheduling
@ComponentScan({"com.example"})
@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
public class MyApplication {
// 一些代码
}
这是我的控制器类:
@RestController
public class MyApplication {
@RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> myApiHandler() {
return new ResponseEntity<String>("成功调用API", HttpStatus.OK);
}
}
我还在application.properties文件中定义了端口如下:
server.port=6069
然而,当我尝试从Postman访问以下API时,我无法从应用程序获得任何响应:
localhost:6069/myApi
这是我收到的错误:
错误:连接ECONNREFUSED 127.0.0.1:6069
我使用Java 8,Spring Boot版本1.5.8和IntelliJ IDE进行开发。您能告诉我我漏掉了什么吗?感谢任何帮助!
英文:
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!
答案1
得分: 0
在@RequestMapping注解中,@RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE) 部分,应该使用 @GetMapping。
@RequestMapping 注解主要用于在类级别进行基本配置以继承行为。
英文:
the problem in @RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE)
you should use @GetMapping.
RequestMapping just for basic config on class level for inherited behavior.
答案2
得分: 0
请使用以下代码:
@GetMapping("/myApi")
而不是:
@RequestMapping(method = RequestMethod.GET, value = "/myApi", produces = MediaType.APPLICATION_JSON_VALUE)
这应该能解决你的问题。
英文:
Use
@GetMapping("/myApi")
instead of
@RequestMapping(method={RequestMethod.GET}, value="/myApi", produces=MediaType.APPLICATION_JSON_VALUE)
This must resolve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论