CXF restful webserice同时支持几种协议 json, xml
来源:互联网 发布:php木马图片制作 编辑:程序博客网 时间:2024/05/17 01:19
1. 假设我们有个服务 (都是从别处拿来的代码)
服务实现类:
import javax.ws.rs.core.Response;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StudentServiceImpl implements StudentService
{
public Student getStudent(long id, String name)
{
Student s = new Student();
s.setId(id);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return s;
}
public Response getStudent1(String name)
{
Student s = new Student();
s.setId(1);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return Response.ok(s).build();
//return s;
}
public UserResponse getStudent(String name)
{
Student s = new Student();
s.setId(1);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return new UserResponse("ok", s);
}
返回数据包装类
普通类
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
@XmlRootElement(name = "Student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student
{
private long id;
private String name;
private Date birthday;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
}
Spring 服务声明
web.xml 就不贴了, 和普通的一样.
2. 访问方法有3种, 可以实现获取不同格式的内容.
http://localhost:8080/student/student/3/info2.json?name=abcss
http://localhost:8080/student/student/3/info2.xml?name=abcss
http://localhost:8080/student/student/3/info2?name=abcss&_type=xml
http://localhost:8080/student/student/3/info2?name=abcss&_type=json
还有一种办法就是在请求时设置Accept:
mport javax.ws.rs.*;
import javax.ws.rs.core.Response;
@Path(value = "/student/{id}")
public interface StudentService
{
@GET
@Path(value = "/info")
Student getStudent(@PathParam("id") long id, @QueryParam("name")
String name);
@GET
@Path(value = "/info2")
UserResponse getStudent(@QueryParam("name") String name);
}
import javax.ws.rs.core.Response;
@Path(value = "/student/{id}")
public interface StudentService
{
@GET
@Path(value = "/info")
Student getStudent(@PathParam("id") long id, @QueryParam("name")
String name);
@GET
@Path(value = "/info2")
UserResponse getStudent(@QueryParam("name") String name);
}
服务实现类:
import javax.ws.rs.core.Response;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class StudentServiceImpl implements StudentService
{
public Student getStudent(long id, String name)
{
Student s = new Student();
s.setId(id);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return s;
}
public Response getStudent1(String name)
{
Student s = new Student();
s.setId(1);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return Response.ok(s).build();
//return s;
}
public UserResponse getStudent(String name)
{
Student s = new Student();
s.setId(1);
s.setName(name);
try
{
s.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1983-04-26"));
}
catch (ParseException e)
{
e.printStackTrace();
}
return new UserResponse("ok", s);
}
返回数据包装类
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserResponse
{
private String status;
private Student data;
public UserResponse()
{
}
public UserResponse(String status, Student data)
{
this.status = status;
this.data = data;
}
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
public Object getData()
{
return data;
}
public void setData(Student data)
{
this.data = data;
}
}
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserResponse
{
private String status;
private Student data;
public UserResponse()
{
}
public UserResponse(String status, Student data)
{
this.status = status;
this.data = data;
}
public String getStatus()
{
return status;
}
public void setStatus(String status)
{
this.status = status;
}
public Object getData()
{
return data;
}
public void setData(Student data)
{
this.data = data;
}
}
普通类
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;
@XmlRootElement(name = "Student")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student
{
private long id;
private String name;
private Date birthday;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
}
Spring 服务声明
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<bean id="rsStudentServiceImpl" class="ex3.StudentServiceImpl" />
<jaxrs:server id="test" address="/student" >
<jaxrs:serviceBeans>
<ref bean="rsStudentServiceImpl" />
</jaxrs:serviceBeans>
<!-- 这里设置了对应关系, 按理说默认就应该是这样, 你可以试试. 当然可以自定义 -->
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</jaxrs:extensionMappings>
</jaxrs:server>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
<bean id="rsStudentServiceImpl" class="ex3.StudentServiceImpl" />
<jaxrs:server id="test" address="/student" >
<jaxrs:serviceBeans>
<ref bean="rsStudentServiceImpl" />
</jaxrs:serviceBeans>
<!-- 这里设置了对应关系, 按理说默认就应该是这样, 你可以试试. 当然可以自定义 -->
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</jaxrs:extensionMappings>
</jaxrs:server>
web.xml 就不贴了, 和普通的一样.
2. 访问方法有3种, 可以实现获取不同格式的内容.
http://localhost:8080/student/student/3/info2.json?name=abcss
http://localhost:8080/student/student/3/info2.xml?name=abcss
http://localhost:8080/student/student/3/info2?name=abcss&_type=xml
http://localhost:8080/student/student/3/info2?name=abcss&_type=json
还有一种办法就是在请求时设置Accept:
HttpGet get = new HttpGet(
"http://127.0.0.1:8080/student/student/3/info2?name=Fetion");
HttpClient httpclient = new DefaultHttpClient();
get.addHeader("ACCEPT", "application/xml");
HttpResponse response = httpclient.execute(get);
StatusLine st = response.getStatusLine();
InputStream ins = response.getEntity().getContent();
byte[] b = new byte[1024];
StringBuilder sb = new StringBuilder();
while (ins.read(b) != -1)
{
sb.append(new String(b, "UTF-8"));
}
System.out.println(sb.toString());
"http://127.0.0.1:8080/student/student/3/info2?name=Fetion");
HttpClient httpclient = new DefaultHttpClient();
get.addHeader("ACCEPT", "application/xml");
HttpResponse response = httpclient.execute(get);
StatusLine st = response.getStatusLine();
InputStream ins = response.getEntity().getContent();
byte[] b = new byte[1024];
StringBuilder sb = new StringBuilder();
while (ins.read(b) != -1)
{
sb.append(new String(b, "UTF-8"));
}
System.out.println(sb.toString());
0 0
- CXF restful webserice同时支持几种协议 json, xml
- CXF restful webserice同时支持几种协议 json, xml... 很简单
- CXF Restful Service(xml,json格式)
- CXF Restful 返回xml
- CXF+Spring+JAXB+Json构建Restful服务
- spring 集成cxf 封装的webserice
- Restful CXF Spring 几种整合一(发布wsdl形式接口)
- cxf 与spring集成,发布restful接口,返回json数据
- SpringMVC基础-8-JSON与Restful支持
- 10.netty开发restful+json协议栈
- XML/JSON的几种解析方式
- Android Volley框架的几种post提交请求方式(xml->xml json->json xml->json)
- CXF Webservice两种方式--soap+restful
- spring+cxf+maven+rest风格+json/xml
- spring mvc restful service string/json/xml
- spring mvc 同时输出json和xml
- CXF 创建 Restful (1) :
- CXF发布restful接口
- log4cpp的安装
- Android中activity、service、broadcast、单例模式http请求的综合使用
- js实现的省市县三级联动的最新源码
- 多线程例子
- 第七章作业
- CXF restful webserice同时支持几种协议 json, xml
- vs2010高亮
- x210v3开发板u-boot-2012.10移植之十一---添加DM9000网卡支持
- 初识JAVA
- 深入理解Android的startservice和bindservice
- Qt之QLineEdit之插入图标及位置设置
- linux 下 mysql-python 安装
- WPF内建面板之——DockPanel
- Remove Element