在使用httpClient来请求Spring mvc接口的时候遇到各种问题,比如返回400(参数异常)或者415.最后无奈只有请同事出手帮忙,终于能顺利调用成功.废话不多说,下面放出代码.
package license.listener;
import license.domain.ApplyDetailBean;
import license.model.SynchronousForm;
import license.service.ApplyDetailService;
import license.service.impl.ApplyDetailServiceImpl;
import license.utils.FactoryIdentNumUtil;
import org.apache.log4j.Logger;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;import javax.servlet.ServletContext;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TimerTask;/**
 * 客户端代码
 */
public class SynchronousTask extends TimerTask {    Logger logger = Logger.getLogger(SynchronousTask.class);
    private static final int STATISTICS_SCHEDULE_HOUR = Integer.parseInt(FactoryIdentNumUtil.getTimeData());
    private static boolean isRunning = false;
    private ServletContext context = null;    ApplyDetailService applyDetailService = new ApplyDetailServiceImpl();    public SynchronousTask(ServletContext context)
    {
        this.context = context;
    }    @Override
    public void run() {
            List<ApplyDetailBean> list = null;
        try {
            list = applyDetailService.findNoAuthCode();
            SynchronousForm synchronousForm = new SynchronousForm();
            synchronousForm.setList(list);            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));            HttpEntity<SynchronousForm> requestEntity = new HttpEntity<SynchronousForm>(synchronousForm, headers);            RestTemplate template = buildRestTemplate();            String url = "http://localhost:8080/applyDetail/getByBat";            ResponseEntity<String> response =
                    template.exchange(url, HttpMethod.POST, requestEntity, String.class);
            System.out.println("响应状态码:" + response.getStatusCode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    private static RestTemplate buildRestTemplate() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();   
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        //设置服务端可以接受的集中参数的格式
        messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        messageConverters.add(new FormHttpMessageConverter());
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
        RestTemplate restTemplate = new RestTemplate(messageConverters);
        restTemplate.setRequestFactory(requestFactory);
        restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
        return restTemplate;
    }
}
package com.ipe.module.dcn.controller;import java.util.ArrayList;
import java.util.List;import com.ipe.module.dcn.entity.ApplyDetail;
import com.ipe.module.dcn.entity.SynchronousForm;
import com.ipe.module.dcn.service.ApplyDetailService;
import com.ipe.module.core.web.controller.AbstractController;
import com.ipe.module.core.web.util.BodyWrapper;
import com.ipe.module.core.web.util.RestRequest;import org.apache.http.HttpMessage;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 *   此处接口需要注意的是    接受参数  synchronousForm   一定要使用@RequestBody来注释 
 */
@Controller
@RequestMapping("/applyDetail")
public class ApplyDetailController extends AbstractController {
    @Autowired
    private ApplyDetailService applyDetailService;
    
    @param RequestMapping(value = {"/getByBat"})
    public
    @ResponseBody
    String GetByBat(@RequestBody SynchronousForm synchronousForm) {
        try {
         LOGGER.info("进入GetByBat接口");
         return "";
        } catch (Exception e) {
            LOGGER.error("",e);
            return "";
        }
    }
}