Building a JSON web service with Java and Axis2
2012-03-17 00:38
232 查看
【http://www.marcusschiesser.de/2009/01/building-a-json-web-service-with-java-and-axis2/】
Building a JSON web service with Java and Axis2
Januar 23rd, 2009 · 9 Comments
It is said that in Java usually theeasiest things are the hardest. The following is a nice example as it took mesome time to figure out how to do it.
I wanted to build a simple URL request based web service in Java that returnsan JSON object. Yes, you can do that with a simple Servlet too, the advantageof using Axis2 isthat you can also call your deployed services
using SOAP without anyconfiguration changes.
1.
Download Axis2 as WAR and install it inyour servlet container
2.
Download the DynamicResponseHandler module andadd it to Axis by copying it to WEB-INF/modules
3. PatchJettison or download my patched version and replaceit with the one installed
in WEB-INF/lib
4. Add the DynamicResponseHandler modulereference to the axis2.xml configuration (located in WEB-INF/conf):
<module ref="DynamicResponseHandler"/>
5. Add the JSON Message formatters to theaxis2.xml:
<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
6. Add JSON Message builders to theaxis2.xml:
<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/>
<messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
7. Start your servlet container and testthe standard version service by calling this url:
http://localhost:8080/axis2/services/Version/getVersion?response=application/json
Now you are ready to add your own webservices. Here you can find an examplehowto deploy a simple POJO service. Have fun!
Update: Zeno (see comments) sent me patch for usage with Jettison 1.2 –otherwise he received a NullPointerException. I haven’t checked it, but
I hopeit helps you! Thanks Zeno!
【http://markmail.org/message/cu2tw43qnrqgqgwp】
Jettison users/dev,
I've started using axis2and enabling JSON support for some
services. (as you know axis2 uses jettison to parse json messages)
I'd like to useparticularly the mapped format with my services,
but when I've tried to use this format, I allways received the same error:
Mapped formatted JSON withnamespaces are not supported in Axis2.
Make sure that your request doesn't include namespaces or use theBadgerfish
convention.
Debugging the code, theerror is generated when the jettison tried
to get the namespace, particularly in MappedNamespaceConvention.java file.
I've modified thisfunction in that way and all started working
fine.
private StringgetJSONNamespace(String ns) {
if (ns == null ||ns.length() == 0) return "";
String jns = (String)xnsToJns.get(ns);
if (jns == null) {
return "";
// throw newIllegalStateException("Invalid JSON namespace: " +
ns);
}
return jns;
}
I've recompiled jettisonand all works fine, and past all unit
test created.
You think this could be a problem forother kinds of messages or
this is a patch that could be applied in future versions.
Regards
Cristian
Building a JSON web service with Java and Axis2
Januar 23rd, 2009 · 9 Comments
It is said that in Java usually theeasiest things are the hardest. The following is a nice example as it took mesome time to figure out how to do it.
I wanted to build a simple URL request based web service in Java that returnsan JSON object. Yes, you can do that with a simple Servlet too, the advantageof using Axis2 isthat you can also call your deployed services
using SOAP without anyconfiguration changes.
1.
Download Axis2 as WAR and install it inyour servlet container
2.
Download the DynamicResponseHandler module andadd it to Axis by copying it to WEB-INF/modules
3. PatchJettison or download my patched version and replaceit with the one installed
in WEB-INF/lib
4. Add the DynamicResponseHandler modulereference to the axis2.xml configuration (located in WEB-INF/conf):
<module ref="DynamicResponseHandler"/>
5. Add the JSON Message formatters to theaxis2.xml:
<messageFormatter contentType="application/json" class="org.apache.axis2.json.JSONMessageFormatter"/>
<messageFormatter contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
6. Add JSON Message builders to theaxis2.xml:
<messageBuilder contentType="application/json" class="org.apache.axis2.json.JSONOMBuilder"/>
<messageBuilder contentType="application/json/badgerfish" class="org.apache.axis2.json.JSONBadgerfishOMBuilder"/>
7. Start your servlet container and testthe standard version service by calling this url:
http://localhost:8080/axis2/services/Version/getVersion?response=application/json
Now you are ready to add your own webservices. Here you can find an examplehowto deploy a simple POJO service. Have fun!
Update: Zeno (see comments) sent me patch for usage with Jettison 1.2 –otherwise he received a NullPointerException. I haven’t checked it, but
I hopeit helps you! Thanks Zeno!
Subject: | [jettison-dev] JSON | |
From: | Cristian Bullokles (cris...@gmail.com) | |
Date: | May 6, 2008 7:34:35 pm | |
List: | org.codehaus.jettison.dev |
I've started using axis2and enabling JSON support for some
services. (as you know axis2 uses jettison to parse json messages)
I'd like to useparticularly the mapped format with my services,
but when I've tried to use this format, I allways received the same error:
Mapped formatted JSON withnamespaces are not supported in Axis2.
Make sure that your request doesn't include namespaces or use theBadgerfish
convention.
Debugging the code, theerror is generated when the jettison tried
to get the namespace, particularly in MappedNamespaceConvention.java file.
I've modified thisfunction in that way and all started working
fine.
private StringgetJSONNamespace(String ns) {
if (ns == null ||ns.length() == 0) return "";
String jns = (String)xnsToJns.get(ns);
if (jns == null) {
return "";
// throw newIllegalStateException("Invalid JSON namespace: " +
ns);
}
return jns;
}
I've recompiled jettisonand all works fine, and past all unit
test created.
You think this could be a problem forother kinds of messages or
this is a patch that could be applied in future versions.
Regards
Cristian
相关文章推荐
- Building a JSON web service with Java and Axis2
- 构建一个RESTful Web Service(Building a RESTful Web Service)
- Spring Building a RESTful Web Service
- Building a RESTful Web Service Using Spring Boot In Eclipse
- 构建一个REST风格的Web服务(Building a RESTful Web Service)
- Building a RESTful Web Service(转)
- DataContractJsonSerializer是在System.ServiceModel.Web.dll中,但命名空间却是System.Runtime.Serialization.Json。
- web test LoadRunner xml / webservice / json / MQ / soapUI / baowen / WebService
- 对Jquery+JSON+WebService的一点认识
- 浅谈对Jquery+JSON+WebService的使用小结
- ExtJs WebService Json序列化(扩展JavaScriptSerializer类)
- ext异步请求一个aspx/ashx/web service取得数据,服务端返回一个json.然后客户端接收并显示.
- 对Jquery+JSON+WebService的一点认识
- ajaxpro jquery json webservice 局部刷新
- Jersey Restful WebService 接收 JSON 数组 对象
- webservice+Jquery返回Json格式
- java webservice 开发方法(REST方式)---续(JSON标准输入输出)
- js+json+webservice(List<>作为参数)
- Getting Started · Building a RESTful Web Service
- Jquery+JSON+WebService使用总结