spring boot的搭建非常简单
先创建mavan项目,在java目录下创建一个启动类application(类名随意),此处application文件必须在最外层,spring boot 只会去扫描application低层数文件中的方法
用SpringBootApplication注解标识为项目启动类:
Application {
main(String[] args) {
SpringApplication.(AccountApplication., args);
}
}
然后添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
再是controller层:
用@Controller或@RestController指明为controller
RestController注解的效果相当于@Controller+@ResponseBody,使用RestController时只需直接return字符串就会把数据返回给前端,
方法部分依旧使用@RequestMapping注解
比如:
@Controller
@RequestMapping(value="/test")
public class test{
@ResponseBody
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String test(){
System.out.print("11111");
return "222222";
}
}
等效于
@RestController
@RequestMapping(value="/test")
public class test{
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String test(){
System.out.print("11111");
return "222222";
}
}
以上spring boot 就算搭好了,spring boot内嵌了tomcat,只需启动main方法,web端就可以访问项目中的方法了
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。