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(飛云小俠) 閱讀(9682) 評論(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)

          相冊

          友情鏈接

          技術網站

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 墨竹工卡县| 延川县| 萍乡市| 磐石市| 枣庄市| 大埔县| 湟中县| 葵青区| 浦县| 从江县| 阜新| 桃园市| 体育| 天水市| 博客| 永善县| 腾冲县| 正阳县| 威远县| 重庆市| 武鸣县| 东明县| 和龙市| 四子王旗| 江源县| 康马县| 阿坝县| 磐石市| 富宁县| 广平县| 张家界市| 舞阳县| 贡觉县| 黔南| 湘潭县| 吉木萨尔县| 眉山市| 亚东县| 平利县| 乌苏市| 哈巴河县|