CXF restful webserice同时支持几种协议 json, xml

来源:互联网 发布:php木马图片制作 编辑:程序博客网 时间:2024/05/17 01:19
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 0
原创粉丝点击