package com.xibei.firstdemo;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/hello")
public class SeController {
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String Say()
{
return "/hello/say 调用的";
}
//请求方式:http://localhost:8080/hello/say/100 -OK
//http://localhost:8080/hello/say?id=100 Error
@RequestMapping(value = "/say/{id}",method = RequestMethod.GET)
public String show(@PathVariable("id") Integer id)
{
return "id:"+id;
//return "/hello/say 调用的";
}
@GetMapping("/show01")
public String show_01(@RequestParam("id") Integer id)
{
return "id:"+id;
//return "/hello/say 调用的";
}
//以下为传通的参数传递方式:如 localhost:8080/hello/say?id=100
//传统的请求方式:http://localhost:8080/hello/show02?id=100
//http://localhost:8080/hello/show02/100 --这种请求方式报错
@RequestMapping(value = "/show02",method = RequestMethod.GET)
public String show_02(@RequestParam("id") Integer myId)
{
return "id:"+myId;
}
//传递默认值 http://localhost:8080/hello/show03?id=23
//http://localhost:8080/hello/show03/23 ---这种方式报错;
@RequestMapping(value = "/show03",method = RequestMethod.GET)
public String show_03(@RequestParam(value="id",required = false,defaultValue = "0") Integer myId)
{
return "id:"+myId;
}
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.Area;
import com.imooc.demo.service.AreaService;
@RestController
@RequestMapping("/superadmin")
public class AreaController {
@Autowired
private AreaService areaService;
/**
* 获取所有的区域信息
*
* @return
*/
@RequestMapping(value = "/listarea", method = RequestMethod.GET)
private Map<String, Object> listArea() {
Map<String, Object> modelMap = new HashMap<String, Object>();
List<Area> list = new ArrayList<Area>();
// 获取区域列表
list = areaService.getAreaList();
modelMap.put("areaList", list);
return modelMap;
}
/**
* 通过区域Id获取区域信息
*
* @return
*/
@RequestMapping(value = "/getareabyid", method = RequestMethod.GET)
private Map<String, Object> getAreaById(Integer areaId) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 获取区域信息
Area area = areaService.getAreaById(areaId);
modelMap.put("area", area);
return modelMap;
}
/**
* 添加区域信息
*
* @param areaStr
* @param request
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
@RequestMapping(value = "/addarea", method = RequestMethod.POST)
private Map<String, Object> addArea(@RequestBody Area area)
throws JsonParseException, JsonMappingException, IOException {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 添加区域信息
modelMap.put("success", areaService.addArea(area));
return modelMap;
}
/**
* 修改区域信息,主要修改名字
*
* @param areaStr
* @param request
* @return
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
@RequestMapping(value = "/modifyarea", method = RequestMethod.POST)
private Map<String, Object> modifyArea(@RequestBody Area area)
throws JsonParseException, JsonMappingException, IOException {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 修改区域信息
modelMap.put("success", areaService.modifyArea(area));
return modelMap;
}
@RequestMapping(value = "/removearea", method = RequestMethod.GET)
private Map<String, Object> removeArea(Integer areaId) {
Map<String, Object> modelMap = new HashMap<String, Object>();
// 修改区域信息
modelMap.put("success", areaService.deleteArea(areaId));
return modelMap;
}
}