前台axios的配置如下:
import axios from 'axios'
import Qs from 'qs'
export default {
get(url, params){
return axios({
method: "get",
baseURL: "/apis",
url,
params,
paramsSerializer: function(params){ //get请求的参数转换为json
return Qs.stringify({"params": JSON.stringify(params)})
},
timeout: 5000,
header:{
'X-Requested-With': 'XMLHttpRequest'
}
})
},
post(url,params){
return axios({
method: "post",
baseURL: "/apis",
url,
data: JSON.stringify(params),
timeout: 5000,
headers: {
"Content-Type": "application/json;charset=UTF-8"
}
})
}
}
前台vue中调用如下:
this.$axios.get("/company/queryByParams", {
"id": this.company.id
}).then(response => {
this.$Message.error('存在下级单位,不允许直接删除')
})
后台resetControll中的方法定义
直接将params参数的值json字符串解析为后台对象,传给jpa进行查询,这里的JSONObject使用alibaba.fastjson,在maven中加入后update即可
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
@ResponseBody
@RequestMapping(value = "/queryByParams", method = RequestMethod.GET, produces = "application/json")
public List<Company> queryByParams(@RequestParam String params) {
Company company = JSON.toJavaObject(JSONObject.parseObject(params), Company.class);
Example<Company> example = Example.of(company);
List<Company> list = companyService.findAll(example);
return list;
}
有其他问题欢迎留言!