春のREST XMLとJSONの例:

春のRESTful WebサービスXMLとJSONの例へようこそ。以前に私はSpring REST JSONについての記事を書きましたが、XMLをサポートするためにプログラムを変更する方法についてたくさんのコメントが寄せられました。また、両方のXMLとJSONをサポートするアプリケーションを作成する方法についてもいくつかのメールを受け取りました。

春の休息XMLとJSON

私は、Spring REST XMLとJSONアプリケーションについての記事を書くことを考えました。そこでは、既存のアプリケーションをXMLをサポートするように簡単に拡張できる方法を紹介します。既存のプロジェクトに変更を加える予定なので、まず以下のリンクからダウンロードしてください。

スプリングのレストフルなウェブサービスプロジェクトをダウンロードしてください。

今、SpringのBeanの設定ファイルに以下の変更を行ってください。

    1. Jaxb2RootElementHttpMessageConverterの種類のBeanを定義してください。

 

上記で設定されたBeanをRequestMappingHandlerAdapterのプロパティmessageConvertersに追加してください。

上記の変更後、私たちの最終的なSpring Beanの設定ファイルは下記のようになります。servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="https://www.springframework.org/schema/mvc"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="https://www.springframework.org/schema/beans"
	xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation="https://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
	
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />

	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	<!-- Configure to plugin JSON as request and response in method handler -->
	<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<beans:property name="messageConverters">
			<beans:list>
				<beans:ref bean="jsonMessageConverter"/>
				<beans:ref bean="xmlMessageConverter"/>
			</beans:list>
		</beans:property>
	</beans:bean>
	
	<!-- Configure bean to convert JSON to POJO and vice versa -->
	<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	</beans:bean>	
	
	<beans:bean id="xmlMessageConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
	</beans:bean>
	
	<context:component-scan base-package="com.scdev.spring.controller" />
	
</beans:beans>

私たちは、クラスでJAXBマーシャリングを行うために、@XmlRootElementアノテーションを付ける必要があることを知っています。したがって、私たちのEmployeeモデルクラスにこれを追加してください。Employee.java

@XmlRootElement
public class Employee implements Serializable{

//no change in code
}

それで終わりです。私たちのSpringアプリケーションは、JSONとXMLの両方をサポートします。それはさらに、JSONの応答があるXMLのリクエストやその逆もサポートします。以下に、これを実演するいくつかのスクリーンショットがあります。注意:私はこのためにPostman Chromeアプリケーションを使っていますが、テストにはどんなRESTクライアントでも使用できます。

1. XMLの応答:Acceptヘッダを「application/xml」として渡してください。
2. JSONの応答:Acceptヘッダを「application/json」として渡してください。
3. XMLのリクエストとJSONの応答:Acceptヘッダを「application/json」、Content-Typeヘッダを「text/xml」として以下の画像に示されるように設定してください。

これで、XMLとJSONの両方をサポートするSpring Restful webサービスの例は以上です。Springフレームワークの拡張はどれだけ簡単か見ることができます。これはSpringフレームワークの人気の主要な理由の一つです。

コメントを残す 0

Your email address will not be published. Required fields are marked *