为什么我无法访问我的服务暴露的REST API?

huangapple go评论63阅读模式
英文:

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: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-starter-web&#39;, version: &#39;1.5.8.RELEASE&#39;)

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({&quot;com.example&quot;})
@SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })
public class MyApplication {
      // Some code
}

and this is my Controller class:

@RestController
public class MyApplication {

       @RequestMapping(method={RequestMethod.GET}, value=&quot;/myApi&quot;, produces=MediaType.APPLICATION_JSON_VALUE)
       public ResponseEntity&lt;String&gt; myApiHandler() {
           return new ResponseEntity&lt;String&gt;(&quot;Successfully called API&quot;, 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(&quot;/myApi&quot;)

instead of

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

This must resolve your issue.

huangapple
  • 本文由 发表于 2023年3月3日 17:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定