java搭建简单的后台服务器(一)
2017-09-13 本文已影响1405人
Charon_Pluto
搭建一个简单地本地后台服务器







- @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!!";
}
}

- 测试

1.下面是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 注入实例。
2.修改传入参数


通过注释设置默认值,就算没有输入参数也会有磨人的值在其中返回


路径变量来进行输入参数的填写

