【问题标题】:RESTful WebService consumes XML,how to call it?RESTful Web Service消费XML,怎么调用?
【发布时间】:2012-06-07 19:34:09
【问题描述】:

更新

我几乎可以完成我的 RESTful 通信,但我还有一些问题:

1 - 如何将我的 XML 分配给连接(下面的代码将举例说明我的情况)?

调用网络服务

public Person getByAccount(Account account) {   
    URL url = new URL(uri);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", "application/xml");

    XStream xstream = new XStream();
    String xmlIn = xstream.toXML(account);

    // Put the xmlIn into the connection

    BufferedReader br = new BufferedReader(new InputStreamReader(
        (connection.getInputStream())));

    StringBuilder sb = new StringBuilder();
    String line;
    while((line = br.readLine()) != null)
        sb.append(line);
    String xmlOut = sb.toString();

    connection.disconnect();
    return (Person) xstream.fromXML(xmlOut);
}

2 - 考虑到最后一个代码示例(Web 服务),下面的类会产生有效的 XML 输出吗?

使用 RESTful 发送的类

@XmlRootElement(name="people")
public class People {
    @XmlElement(name="person")
    public List<Person> people;

    public People() {
        people.add(new Person(1, "Jan"));
        people.add(new Person(2, "Hans"));
        people.add(new Person(3, "Sjaak"));
    }

    public List<Person> all() {
        return people;
    }

    public Person byName(String name) {
        for(Person person : people)
            if(person.name.equals(name))
                return person;

        return null;
    }

    public void add(Person person) {
        people.add(person);
    }

    public Person update(Person person) {
        for(int i = 0; i < people.size(); i++)
            if(person.id == people.get(i).id) {
                people.set(i, person);
                return person;
            }

        return null;
    }

    public void remove(Person person) {
        people.remove(person);
    }
}

网络服务

@GET
@Path("/byAccount")
@Consumes("application/xml")
@Produces("application/xml")
public Person getByAccount(Account account) {
    // business logic
    return person;
}

【问题讨论】:

    标签: java web-services rest call


    【解决方案1】:

    试试这个:

    conn.setDoOutput(true);
    OutputStream output = conn.getOutputStream();
    // And write your xml to output stream.
    

    查看此链接以使用标准的 REST URLhttp://rest.elkstein.org/2008/02/using-rest-in-java.html

    编辑

    首先,您需要将您的getByAccount 请求更改为POST 请求,因为GET 请求不允许在正文中传递任何信息,它仅使用url 中的请求参数。但是你发送 XML,所以使用POST

    尝试以下版本的发送方法:

    public Person getByAccount(Account account) {   
        URL url = new URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/xml");
        connection.setOutput(true);
    
        XStream xstream = new XStream();
        xstream.toXML(account, connection.getOutputStream());
    
        Person person = (Person) xstream.fromXML(connection.getInputStream());   
        connection.disconnect();
        return person;
    }
    

    【讨论】:

    • 我确实见过很多使用 InputStream 和 OutputStream 的例子。虽然,在上面的情况下(更新了我的问题)我不知道如何让它工作。我该如何解决这种情况?感谢您迄今为止的帮助!
    • 谢谢尼基塔!这个解决方案简化了我的代码并且做得很好。
    • 如果他的请求是幂等的,他不应该使用 PUT 而不是 POST 吗?
    • @MohamedSalig 链接已修复nabisoft.com/tutorials/java-ee/…
    【解决方案2】:

    您可以使用Jersey Client API, (one more link) 来进行最充分的通话。

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2013-09-26
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多