http request response json 测试 Spring MVC

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import com.qiyuexinxi.model.OrderModel;

public class ClientTest {

static InputStream input = null;// 输入流
static InputStreamReader isr = null;
static BufferedReader buffer = null;
static StringBuffer sb = null;
static String line = null;

public static void main(String[] args) {

OrderModel order = new OrderModel();
order.setCourse_id("0000000002");
order.setUser_id("0000000000006");
order.setFee(209);
JSONObject jsonObject = new JSONObject(order);

HttpPost httpPost = new HttpPost("http://localhost:8080/initOrder");
StringEntity entity = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
// 若状态值为200,则ok
if (code == HttpStatus.SC_OK) {
// 从服务器获得输入流
input = response.getEntity().getContent();
isr = new InputStreamReader(input);
buffer = new BufferedReader(isr, 10 * 1024);

sb = new StringBuffer();
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
}
} catch (Exception e) {
// 其他异常同样读取assets目录中的"local_stream.xml"文件
e.printStackTrace();
} finally {
System.err.println(sb.toString());
try {
if (buffer != null) {
buffer.close();
buffer = null;
}
if (isr != null) {
isr.close();
isr = null;
}
if (input != null) {
input.close();
input = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


package com.qiyuexinxi.util;

import javax.validation.Valid;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.qiyuexinxi.model.OrderModel;
import com.qiyuexinxi.service.OrderService;

@RestController
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan(basePackages="com.qiyuexinxi")
public class Application {

@Autowired
private OrderService orderService;

@RequestMapping(value="/initOrder", method = RequestMethod.POST)
public @ResponseBody OrderModel initOrder(@Valid @RequestBody final OrderModel data) {
return orderService.initWXOrder(data);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值