使用Spring REST模板POST POST JSON对象

时间:2016-07-25 15:13:52

标签: json spring rest spring-mvc spring-rest

我使用Spring rest模板发布JSON对象。它适用于较少的数据,但发布更多数据会导致Request URI错误太长。

       final String url = getServiceUrl() + "/rs/doc?param1=test";

        RestTemplate restTemp=getRestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);

        //set your entity to send
        HttpEntity<MyBean> request = new HttpEntity<MyBean>(myBean,headers);

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        messageConverters.add(new MappingJacksonHttpMessageConverter());
        messageConverters.add(new FormHttpMessageConverter());
        restTemp.getMessageConverters().addAll(messageConverters);

        // send it!
        responseEntity = restTemp.exchange(url, HttpMethod.POST, request, String.class);

请求正文应该在POST方法中接受无限数据。但这似乎并不适用于此。有人可以指导。

1 个答案:

答案 0 :(得分:1)

以下对我来说工作正常。我在标题和我需要发送的帖子参数中添加了安全性详细信息。

const path = require('path');
const fsp = require('fs-promise');

function addPages(startAt, pages, mode) {
    let htmlExt = mode.HTML;
    let cssExt = mode.CSS;

    return fsp.readFile(path.join('.', 'templates', 'body.html'), { encoding: 'utf-8' })
        .then((content) => {
            var pendingWrites = [];

            for (let i = startAt, endAt = startAt + pages; i < endAt; i++) {
                let filename = path.join('.', 'manuscript', `page-${i}`, `style.${cssExt}`);
                let thisWrite = fsp.writeFile(filename, '');

                pendingWrites.push(thisWrite);
            }

            return Promise.all(pendingWrites);
        })
        .catch((err) => {
            // either fully recover from the error or rethrow
            console.log("Could not add pages: ", err);
            throw err;
        });
}