1:创建项目
new->project然后选择左侧Spring Initizlizr: 录入gav等信息: 然后next,springboot选择2.2.10版本,依赖选择spring-web: 然后next->finish,最终项目结构如下:
2:添加接口并测试
2.1:创建HelloWorldController
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String
index() {
return "Greetings from Spring Boot !";
}
}
2.2:修改端口号
默认端口号为8080,通过修改application.properties修改默认端口为9876:
server.port=9876
2.3:启动服务
直接运行SpringbootHelloworldApplication:
2.4:访问服务
3:测试用例测试
@RunWith(SpringRunner
.class)
@SpringBootTest(webEnvironment
= SpringBootTest
.WebEnvironment
.DEFINED_PORT
)
public class HelloControllerIT {
@LocalServerPort
private int port
;
private URL base
;
@Autowired
private TestRestTemplate template
;
@Before
public void setup() throws Exception
{
this.base
= new URL("http://localhost:" + port
+ "/hello");
}
@Test
public void getHello() throws Exception
{
ResponseEntity
<String> response
= template
.getForEntity(base
.toString(), String
.class);
assertEquals(
"Expect <Greetings from Spring Boot!>",
"Greetings from Spring Boot !",
response
.getBody()
);
}
}
4:源码
这里,如无积分可评论联系我。
最后:都让开,我要喝瑞幸