SpringMVC中的RestFul

SpringMVC中的RestFul的使用

在URL风格中
RestFul的好处主要为简洁,高效和安全。因为在原生的URL风格中,页面传递参时,总会显示传递的属性和值。

	localhost:8080/method?a=1&b=1

而RestFul则可以避免这种情况,这主要就是RestFul的安全性的体现

	localhost:8080/method/1/2

SpringMVCRestFul有两种方式的运用,主要都是要用到**@PathVariable**注解来实现

第一种方法:

package com.leige.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leige.domain.Student;
import com.leige.service.StudentService;

@Controller
public class ControllerRestFul{
	//RequestMethode.可以设置RequestMapping的请求方式:get put post delet
	//RequestMapping中value可以由Path代替,这两个的意义是一样的
	@RequestMapping(value = "/test1/{a}/{b}",method = RequestMethod.GET)
	public String test(@PathVariable int a ,@PathVariable int b ,Model model){
		int c = a + b;
		model.addAttribute("msg","a+b="+c);
		return "test" //视图页面的地址
	}
}

上面的URL格式就可以为:localhost:8080/test/test1/1/2
而页面上就能得出: a+b = 3的内容

第二种方法:

`

package com.leige.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leige.domain.Student;
import com.leige.service.StudentService;

@Controller
public class ControllerRestFul{
	/*不同的请求方式对应的着不同的Mapping
		GetMapping
		PostMapping
		PutMapping
		DeleteMapping
		*/
	@GetMapping("test2/{a}/{b}")
	public String test(@PathVariable int a ,@PathVariable int b ,Model model){
		int c = a + b;
		model.addAttribute("msg","a+b="+c);
		return "test" //视图页面的地址
	}
}

第二种RestFul的抒写格式也是最简洁最多使用的方式
不同的请求方式,但要是URL格式一样,也会导致类似于重载的情况出现也就是说可以根据视图表单的请求方式不同,来走不一样的Controller方法。

使用注解开发SpringMVC

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值