RestOperations使用建議

RestOperations

<!-- TOC -->java

<!-- /TOC -->json

some advices about ESG provider and consumerapp

public <T> T postForObject(String url, Object request, Class resultClass);

MultiValueMap

MultiValueMap param = new LinkedMultiValueMap();
param.add("k1", "v1");
param.add("k2", "v2");
param.add("k2", "v2v2");
param.add("k3", "v3");

// FormHttpMessageConverter
setContentType("application/www-form-data-urlencode")

// k1=v1&k2=v2&k2=v2v2&k3=v3

// controller
request.getParameter(String name);
request.getParameterMap();

Map/Object(canSerializable)

RestTemplate will converte request object to JSONide

setContentType("application/json")

With Spring MVC @RequestBody annotation in controller method(handler), Spring MVC GsonHttpMessageConverter or GsonHttpMessageConverter etc.post

// for controller method parameter annotated with RequestBody
Object body = readFromBodyInputStream(request);

Service provider-Pafa3

No HttpMessageConverter for Pafa3, so we can only use HttpServletRequest#getParameter to get parameter or read request body by HttpServletRequest#getInputStream.this

But some one did some stupid things, es: SalesEsgFilter, they read the input stream, to do some customized operations such as checking the sign(is it necessary?), and controller can not get input stream anymore.url

If still want to check sign in Filter, take this solution:spa

public class JsonBodyHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private Map<String, Object> jsonParam;

    public JsonBodyHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public JsonBodyHttpServletRequestWrapper(HttpServletRequest request, Map<String, Object> jsonParam) {
        super(request);
        this.jsonParam = jsonParam;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        // TODO: 2018/4/5 與 getReader 只能調用一次
        return null;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        // TODO: 2018/4/5
        return null;
    }
}

or move JSON parameters to HttpServletRequest#getParameter, over ride getParameter, getParameterMap, etc method.code

consumer

depend on the way provider get parameter. HttpServletRequest#getParameter: MultiValueMap @RequestBody: Map or other can serializable objectorm

相關文章
相關標籤/搜索