Spring MVC +JSON


コントローラ内で@ResponseBodyを使用してフロントに関連情報を返します
@Controller
public class JsonController {
		@RequestMapping("/json")
		@ResponseBody
		public List<User> getUser(){
			List<User> list = new ArrayList<User>();
			User u = null;
			for(int i =0; i < 20000; i++){
				u = new User();
				u.setId(i);
				u.setMsg(i+"aaaaaaaaa");
				u.setUname(i+"admin");
				u.setDate(new Date());
				list.add(u);
			}
			return list;
		}
}

1)jackson
<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
				<!--   ie    -->
				<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
			</bean>
		</mvc:message-converters>
</mvc:annotation-driven>

2)fastjson
<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
				<property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
				<property name="features">
					<array>
						<value>WriteMapNullValue</value>
						<value>WriteNullStringAsEmpty</value>
					</array>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

注意:
1.
@ResponseBody:コンテンツまたはオブジェクトをHTTP応答本文として返し、@ResponseBodyを使用するとビュー処理部がスキップされ、適切なHttpMessageConverterが呼び出され、戻り値が出力ストリームに書き込まれます.
2.プロファイルのヘッダのxsdファイル3.0のサポートされていないにはサブアイテム構成があります.4.0未満またはspring-beans.xsdを変更(デフォルトは最新)
3.シーケンス化時の時間フォーマットの問題を解決し、デフォルトはlongタイプに解析されるミリ秒である.ここでは実体クラスで@JSOnField(format="yyyy-MM-dd HH:mm:ss")を使用することができる.
@JSONField(format="yyyy-MM-dd HH:mm:ss") private Date date;