实现用http请求对接spring mvc接口

睡不醒的加菲 2017-02-12 10:33:50
在使用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 "";
}
}
}


...全文
471 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
clever101 2019-08-05
  • 打赏
  • 举报
回复
楼主,你这不需要在配置文件中指定Action类吗?
clever101 2019-08-05
  • 打赏
  • 举报
回复
引用 4 楼 睡不醒的加菲 的回复:
引用 3楼小灯光环 的回复:
貌似不需要这么麻烦啊
有更好的方法
更好的方法的是什么?
睡不醒的加菲 2017-02-15
  • 打赏
  • 举报
回复
引用 3楼小灯光环 的回复:
貌似不需要这么麻烦啊
有更好的方法
小灯光环 2017-02-15
  • 打赏
  • 举报
回复
貌似不需要这么麻烦啊
睡不醒的加菲 2017-02-15
  • 打赏
  • 举报
回复
???这是几个意思
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧