普通web项目
- 使用软件:IDEA
- 新建项目,file->new->project
- 点击next,配置自己的项目信息,其他不用管
- 继续next,选择web->spring web
- next,不用做修改,直接finish
- 在com.leon.zero_one包下新建一个package,命名为controler,包下新建类:HelloController
package com.leon.zero_one.controler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
@Controller
public class HelloController {
@RequestMapping("/")
@ResponseBody
public String getHello() {
return "hello Spring Boot!这是一个用Spring Boot开发的网站。";
}
}
- 点击运行,在浏览器地址栏输入
localhost:8080
查看效果
Restful风格的web接口
- 使用软件:IDEA
- 新建一个web项目
- 功能:简单的计数器,实现初始化计数器,修改计数器和查询计数器
- 构造下面这样的项目结构
- 写入代码
Count:
package com.couter.demo.bean;
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
ResourceController:
package com.couter.demo.controller;
import com.couter.demo.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.couter.demo.service.ResourceService;
@RestController
public class ResourceController {
@Autowired
ResourceService resourceService;
@RequestMapping(value = "/me/count", method = RequestMethod.PUT)
@ResponseBody
public void initCount(@RequestBody Count count){
resourceService.initCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.POST)
@ResponseBody
public void modifyCount(@RequestBody Count count){
resourceService.addCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.GET)
@ResponseBody
public Count getCount()
{
return resourceService.getCount();
}
}
ResourceService:
package com.couter.demo.service;
import com.couter.demo.bean.Count;
import com.couter.demo.manager.ResourceManager;
import org.springframework.stereotype.Service;
@Service
public class ResourceService {
public void addCount(Count count){
if (count != null){
ResourceManager.getInstance().addCount(count.getCount());
}
}
public void minusCount(Count count){
if (count != null) {
ResourceManager.getInstance().minusCount(count.getCount());
}
}
public Count getCount()
{
Count count = new Count();
count.setCount(ResourceManager.getInstance().getCount());
return count;
}
public void initCount(Count count){
if (count != null) {
ResourceManager.getInstance().initCount(count.getCount());
}
}
}
ResourceManager:
package com.couter.demo.manager;
public class ResourceManager {
private int count = 0;
private static ResourceManager instance = new ResourceManager();
private ResourceManager(){}
public static ResourceManager getInstance(){
return instance;
}
public synchronized void addCount(int i){
count = count + i;
}
public synchronized void minusCount(int i){
count = count -i;
}
public int getCount(){
return count;
}
public void initCount(int i){
count = i;
}
}
DemoApplication是IDEA自动生成的,无需修改
- 启动服务
在DemoApplication类里点击运行,服务启动正常
测试
- 使用软件:PostMan
- url统一填/me/count,共使用三个接口进行测试,分别为get,post,put。get查询初始值为0:
- 初始化并修改
初始化为100
查询
修改
再次查询
完成!