java手把手教你搭建简单的后台服务器(一)
2017-09-19 本文已影响1489人
代码界的扫地僧
搭建一个简单地本地后台服务器








然后 点Next和Finish

-
@RestController 标志这是一个控制器
-
@ResponseBody 会包装返回结果
-
@RequestMapping 是匹配前台请求路径的
package com.demo.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//公开一个方法hello
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello spring boot!!";
}
}




spring是把注解玩的最6的一个框架
下面是Spring最著名的一个功能 依赖注入

package com.demo.springboot.service;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
public String sayHello(){
return "Hello this is Service";
}
}





从main方法开始看,SpringApplication.run 传进去了一个class对象,通过反射 拿到了类的Package(com.demo.springboot),然后会扫描包下的所有类。拿到类的注解 @RestController @Component @Service,那么就会构建这个类 newInstance,然后扫描类的所有字段,保护@Autowired 注入实例。
修改传入参数



下面是参数注解 加入默认值操作
通过注释设置默认值,就算没有输入参数也会有默认的值在其中返回







