java后台写http请求,http各种请求java后台处理

不定时更新

GET 查询(有点长)

final private StatusCode successCode = new StatusCode(200, "OK", true);

final private int pageCount = 10000;

@RequestMapping(value = "/image", method = RequestMethod.GET)

public @ResponseBody

JSONObject image(String TYPE) throws Exception {

JSONObject json = new JSONObject();

PageData pd = new PageData();

Page page = new Page();

try {

if(null == TYPE || "".equals(TYPE)) throw new ParamsErrorException();

pd.put("TYPE", TYPE.trim());

page.setShowCount(pageCount);

page.setPd(pd);

ListresultData = cmpIntroService.list(page);

if (null == resultData || resultData.size() < 1) throw new QueryFailedException();

json.putAll(json.fromObject(successCode));

json.put("data", resultData);

return json;

} catch (ParamsErrorException e) {

return json.fromObject(e.getSc());

} catch (QueryFailedException e) {

return json.fromObject(e.getSc());

}

}

ParamsErrorException

package com.controller.api;

import com.entity.api.StatusCode;

/**

* @param

* @author: Liweihua

* @date: 2019-04-19 09:14

* @return

*/

public class ParamsErrorException extends Exception {

//无效的参数

//错误码:400 错误信息:invalid parameters

StatusCode sc = new StatusCode(400, "invalid parameters", false);

public ParamsErrorException() {

}

public ParamsErrorException(int code, String msg, boolean status) {

sc.setCode(code);

sc.setMsg(msg);

sc.setStatus(status);

}

public StatusCode getSc() {

return sc;

}

}

QueryFailedException

package com.controller.api;

import com.entity.api.StatusCode;

/**

* @param

* @author: Liweihua

* @date: 2019-04-19 09:14

* @return

*/

public class QueryFailedException extends Exception {

//无效的参数

//错误码:404 错误信息:no result

StatusCode sc = new StatusCode(404, "no result", false);

public QueryFailedException() {

}

public QueryFailedException(int code, String msg, boolean status) {

sc.setCode(code);

sc.setMsg(msg);

sc.setStatus(status);

}

public StatusCode getSc() {

return sc;

}

}

StatusCode

package com.entity.api;

/**

* @param

* @author: Liweihua

* @date: 2019-04-18 17:37

* @return

*/

public class StatusCode {

private int code;//200 : OK; 500 : 代码错误; 404 : Not Found;

private String msg;

private boolean status;

public StatusCode(int code, String msg, boolean status) {

this.code = code;

this.msg = msg;

this.status = status;

}

public int getCode() {

return code;

}

public void setCode(int code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public boolean isStatus() {

return status;

}

public void setStatus(boolean status) {

this.status = status;

}

}

POST

form表单

eadc6a9edb3194f024b92a1c9c8f45a1.png

后台正常取parameter即可;

json格式

参考来源:Java后台如何接收并处理前台传过来的json格式的数组参数

661efd884d503d78ff025499e32c58f6.png

后台处理:参数列表是@RequestBody

@RequestMapping(value = "/addcart", method = RequestMethod.POST)

public @ResponseBody

JSONObject addcart(@RequestBody JSONObject obj) throws Exception {

JSONObject json = new JSONObject();

PageData pd = this.getPageData();

Page page = this.getPage();

try {

if(pd.size()<1) throw new ParamsErrorException();

json.putAll(json.fromObject(successCode));

return json;

} catch (ParamsErrorException e) {

return json.fromObject(e.getSc());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值