`

http request response json 测试 Spring MVC

    博客分类:
  • java
阅读更多
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);
	}
}
分享到:
评论

相关推荐

    Spring MVC 中几种获取request和response的方式

    Spring MVC 中几种获取request和response的方式,讲解详细实用!

    Spring MVC 入门实例

    首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用程序接收到 http://localhost:8080/hello.do(事实上请求路径是 /hello.do) 的请求后, Spring 将这个请求交给一个名为 helloController ...

    spring MVC 配置文档

    DispatcherServlet 是Spring MVC 的入口 所有进入Spring Web 的 Request 都经过 DispatcherServlet 需要在 web.xml 中注册 DispatcherServlet <servlet> <servlet-name>dispatherContext</servlet-name> ...

    Spring MVC 3.0实战指南.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及解析器 7...

    微信小程序跳转传参数 传对象,wx.request的json数据传输

    我的解决办法是先将对象转换为json字符串然后到下个页面将json字符串,再转化为对象。如下:   let str=JSON.stringify(e.currentTarget.dataset.item); wx.navigateTo({ url: '../toMybaby/babyDetail/babyDetail?...

    Spring3MVC注解教程.ppt

    《Spring MVC 3.0实战指南》,参考《Spring 3.x企业应用开发实战》。 内容简介: 1、Spring MVC框架简介 2、HTTP请求地址映射 3、HTTP请求数据的绑定 4、数据转换、格式化、校验 5、数据模型控制 6、视图及...

    Spring MVC Cookbook(PACKT,2016)

    Spring MVC is a lightweight application framework that comes with a great configuration by default. Being part of the Spring Framework, it naturally extended and supported it with an amazing set of ...

    spring mvc 3.2 参考文档

    Spring MVC 框架简介 Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view ...

    Spring mvc 接收json对象

    本文通过代码实例介绍spring mvc 接收json数据的方法,具体详情如下所示: 接收JSON 使用 @RequestBody 注解前台只需要向 Controller 提交一段符合格式的 JSON,Spring 会自动将其拼装成 bean。 1)在上面的项目中...

    spring_mvc_request

    spring_mvc_request

    request response

    request response

    springmvc demo

    页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)的实现(也可以是任何的POJO类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常...

    spring mvc 拦截器获取请求数据信息.rar

    spring mvc 拦截器获取请求数据信息 解压之后放到项目中 直接运行就可以了 (将流多次运用)

    Spring MVC打印@RequestBody、@Response日志的方法

    主要介绍了Spring MVC打印@RequestBody、@Response日志的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring MVC参数校验详解(关于`@RequestBody`返回`400`)

    主要介绍了Spring MVC参数校验的相关资料,主要是针对`@RequestBody`返回`400`的问题,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。

    Spring.MVC.Cookbook.1784396419

    You will analyze how Spring MVC responds to complex HTTP requests. You will implement Hypermedia and HATEOAS to guide your customer's stateless conversation with the product and see how a messaging-...

    Spring mvc实现

    Spring mvc Spring 注解 ibatis serviceRequest

    request-json:Http Client轻松处理JSON API

    request = require ( 'request-json' ) ; var client = request . createClient ( 'http://localhost:8888/' ) ; var data = { title : 'my title' , content : 'my content' } ; client . post ( 'posts/' , data...

    Spring mvc中 RequestMapping 6个基本用法小结

    Spring mvc中@RequestMapping 6个基本用法小结

    Spring MVC之@RequestMapping详解

    前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/json, 而且服务器端通过request....

Global site tag (gtag.js) - Google Analytics