springMVC(九)——restful风格和异常处理

restful风格

REST架构是一个抽象的概念,目前主要是基于HTTP协议实现,其目的是为了提高系统的可伸缩性,降低应用之间的耦合度,便于框架分布式处理程序。

Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

它改变了URL的地址风格

举个例子:以前的URL为:http://localhost:8080/Demo/list/login?id=1;

而使用restful风格后的地址变为:http://localhost:8080/Demo/list/1;

 

它使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。分别对应 添加、 删除、修改、查询。

package com.zhiyou100.zjc.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
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.servlet.ModelAndView;

import com.zhiyou100.zjc.bean.User;

@Controller
@RequestMapping("user")
public class UserController {
    //method:表示该方法处理GET请求
    //uid:接受参数
    @RequestMapping(value="{uid}",method = RequestMethod.GET)
    public String findById(@PathVariable("uid") int id) {//把uid赋值给id
        System.out.println(id);
        return "index";
    }
    @RequestMapping(value = "{uid}",method = RequestMethod.POST)
    public String insertUser(@PathVariable("uid" ) int id,User user) {
        System.out.println(user);
        return "index";
    }
    //springmvc提供了一个人过滤,该过滤器可以把post请求转化为put和delete请求
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public String update(User user) {
        System.out.println(user+"--------update");
        return "index";
    }
    @RequestMapping(value="{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable int id) {
        
        System.out.println(id+"---delete");
        return "index";
    }    
}

在使用put和delete方法时候,要在web.xml中配置过滤器

    <filter> <filter-name>hiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

 Springmvc如何进行异常处理

异常处理分为局部异常处理和全局异常处理

局部异常处理:

@ExceptionHandler//局限于当前的类中
    public ModelAndView error(Exception exception) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("error", exception.getMessage());
        return mav;
    }

 

 全局异常处理,是定义一个全局异常类

package com.zhiyou100.zjc.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler
    public ModelAndView error(Exception exception) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("error");
        mav.addObject("error", exception.getMessage());
        return mav;
    }
}

 

转载于:https://www.cnblogs.com/zjc364259451/p/11469630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值