如何使springboot注册RestController?

时间:2020-10-14 01:12:29

标签: spring-boot rest

我以前没有对Spring API工具进行过多的工作,对Jersey有了更多的经验。我看过许多不同的文章,这些文章解释了如何通过Springboot连接REST端点,但我看不出为什么这不起作用。

以下是一些相关的代码段:

在application.properties中

server.port=5000

该软件包是com.myproj

@SpringBootApplication(scanBasePackages={"com.myproj"})
public class ClaimsIntakeServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClaimsIntakeServiceApplication.class, args);
    }

}

在com.myproj.controllers中

@Controller
@RequestMapping(value = "/hello")
public class ClaimController {


    @GetMapping
    public ResponseEntity<Item> read() {
        System.out.println("hi");
        return new ResponseEntity<>("", HttpStatus.OK);
    }

如果我在邮递员中点击此URL:https:// localhost:5000 / hello .. 它说

连接被拒绝

如何用springboot连接休息端点,以便可以用邮递员按上面所示命中它?

2 个答案:

答案 0 :(得分:1)

类似的事情应该起作用:

主类:

@SpringBootApplication
@ComponentScan(basePackages = {"com.myproj"})
public class HelloWorldApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

控制器:

@RestController
public class HelloWorldController {


@RequestMapping(path = "/hello-world", method = RequestMethod.GET, produces = 
MediaType.APPLICATION_JSON_VALUE)
    public HelloWorldResponse getHelloWorld() {
        return new HelloWorldResponse("hello!!");
    }
}

文档here

答案 1 :(得分:0)

出于某种原因,我通过以下方式获取了API的所有内容:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>

为了使API能够正常工作,我还必须包括此内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>