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); } }