关于context-type

1、context-type

(1)get请求

get请求直接在地址中拼接参数即可

get请求直接@RequestParam就可以获取

(2) post请求

前台数据格式

var jsonParam = {id:id,name:name}

context-type:application/json

method: 'post'

后台接收(注意,@RequestBody只能在post请求中使用)

用 @RequestBody 就可获得封装的实体

这样就可以获得前台传来的对象信息,但是有时候想从前台传递的json对象中获取单个的属性值,不想封装对象,该怎么做呢?

前台数据

var jsonParam = {id:id, name: name}

context-type: application/x-www-form-urlencoded

method: post

后台接收

public String find(@RequestParam(name="id") String id){}

前台的json在后台默认已经封装成对象,直接取参数是取不到的,

需要将context-type修改为x-www-form-urlencoded

这样做的目的客户端会将jsonParam对象的格式编码为key/value的格式发送到服务器

通过@RequestParam就能获取到


猜你喜欢

转载自www.cnblogs.com/newbest/p/11210258.html
今日推荐