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

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

简单吧.... 呵呵


posted on 2010-07-20 00:30 Scud(飞云小侠) 阅读(9656) 评论(1)  编辑  收藏 所属分类: SOA

评论

# re: CXF restful webserice同时支持几种协议 json, xml... 很简单 2012-08-30 16:58 jiangli

服务器端参数是个对象形式,客户端传过去对象的json格式,能不能接收?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

公告

文章发布许可
创造共用协议:署名,非商业,保持一致

我的邮件
cnscud # gmail


常用链接

留言簿(15)

随笔分类(113)

随笔档案(103)

相册

友情链接

技术网站

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜