CXF restful webserice同时支持几种协议 json, xml... 很简单

34 篇文章 0 订阅
1. 假设我们有个服务 (都是从别处拿来的代码)

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;
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;
}
}


普通类


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>


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());


简单吧.... 呵呵
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值