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);
            List resultData = 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表单

http各种请求java后台处理_第1张图片

后台正常取parameter即可;

json格式

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

http各种请求java后台处理_第2张图片

后台处理:参数列表是@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());
        }
    }

 

你可能感兴趣的:(java,http)