iT邦幫忙

DAY 12
0

無痛學習SpringMVC與Spring Security系列 第 12

[Controller/View]資料直接輸出JSON以及XML格式(I, ContentNegotiatingViewResolver)

Spring MVC中要將資料直接以JSON以及XML格式輸出,Spring有兩種方式可以達成,一是透過ContentNegotiatingViewResolver,二是透過HttpMessageConverters,先介紹ContentNegotiatingViewResolver,

介紹前ContentNegotiatingViewResolver,請先加入以下dependencies,另外看官方文件的時候,發現springframework版本換成4.1.1也不影響,另外4.0.x到4.1.x有一些improvement或許會用到或有空試試看:

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-orm</artifactId>
	<version>${spring.version}</version>
</dependency>
<dependency>
	<groupId>javax.xml.bind</groupId>
	<artifactId>jaxb-api</artifactId>
	<version>2.2.11</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>${jackson.json.version}</version>
</dependency>

<properties>
       <jackson.json.version>2.4.3</jackson.json.version>
       <spring.version>4.1.1.RELEASE</spring.version>
</properties>

Spring MVC Controller可以指定輸出的HTTP Content,如application/xml, application/json,ContentNegotiatingViewResolver delegate org.springframework.web.servlet.view.json.MappingJackson2JsonView以及org.springframework.web.servlet.view.xml.MarshallingView,前者需要Jackson JSON dependency,後者可以用Spring OXM來進Marshall物件,首先在dispatcherservletcontext.xml加入相關bean如下:

<bean id="jsonView"
		class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
		<property name="prettyPrint" value="true" />
	</bean>

	<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
		<property name="classesToBeBound">
			<list>
				<value>tw.blogger.springtech.springmvc.model.DCN</value>
			</list>
		</property>
	</bean>
​
	<bean id="xmlView"
		class="org.springframework.web.servlet.view.xml.MarshallingView">
		<property name="marshaller" ref="jaxbMarshaller"/>
	</bean>

	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="defaultViews">
			<list>
				<ref bean="jsonView" />
				<ref bean="xmlView" />
			</list>
		</property>
	</bean>

o.s.oxm.jaxb.Jaxb2Marshaller需要指定要Marshall的Class,同時對應的Model Class也要宣告@XmlRootElement的annotation,另外檔案類型的mutilpart是沒辦法Marshall的,不Marshall的屬性要宣告@XmlTransient,model class code修改部分如下,

@XmlRootElement(name="DCN")
@Entity
//預設Table name即為class name, 如果要指定table name,則需加入@Table(name="tablename")
public class DCN implements Serializable{
	....
	@XmlTransient
	@JsonIgnore
	public MultipartFile getUploadFile() {
		return uploadFile;
	}
	...
}

JSON的部分則無需特別設定,僅設定一屬性prettyPrint,使印出JSON格式較為可讀,

啟動Server,網址:http://localhost:8080/SpringMVC/dcn.json,需注意要指定副檔名,告訴ContentNegotiatingViewResolver,要render哪種view呈現,畫面如下

觀察console log,

Hibernate: select dcn0_.prikey as prikey1_0_, dcn0_.category as category2_0_, dcn0_.completedDate as complete3_0_, dcn0_.issuedDate as issuedDa4_0_, dcn0_.no as no5_0_, dcn0_.rev as rev6_0_, dcn0_.trackNumber as trackNum7_0_ from DCN dcn0_
21:39:20 [http-nio-8080-exec-6] ContentNegotiatingViewResolver - Requested media types are [application/json] based on Accept header types and producible media types [*/*])
21:39:20 [http-nio-8080-exec-6] ContentNegotiatingViewResolver - Returning [org.springframework.web.servlet.view.json.MappingJackson2JsonView: name 'jsonView'] based on requested media type 'application/json'
21:39:20 [http-nio-8080-exec-6] DispatcherServlet - Rendering view [org.springframework.web.servlet.view.json.MappingJackson2JsonView: name 'jsonView'] in DispatcherServlet with name 'dispatcher'
21:39:20 [http-nio-8080-exec-6] DispatcherServlet - Successfully completed request

可以看到呼叫json view,來輸出畫面

明天繼續介紹透過HttpMessageConverters來達到相同的功能,但不同的方式來達到相同的目的。


上一篇
[Controller]混用xml組態檔及Java Config,"Handler Method Not Found"問題暫時解決
下一篇
[Controller/View]資料直接輸出JSON以及XML格式(II, @ResponseBody/HttpMessageConverter/@RestController)
系列文
無痛學習SpringMVC與Spring Security31

尚未有邦友留言

立即登入留言