从Java调用Restful Service

Ris*_*shi 15 java rest web-services

在这里,我不是在创建RESTful服务,我必须从我的java代码中调用外部Restful服务.目前我正在使用Apache HttpClient实现这一点.我从Web服务获得的响应是​​XML格式.我需要从XML中提取数据并将它们放在Java对象上.我听说我们可以使用JAX-RS和JERSEY来自动将xml标签映射到相应的java对象,而不是使用SAX解析器.

我一直在寻找但无法找到入门的来源.我确实查看了现有的链接 使用Java中的Java RESTful调用来使用RESTful API

任何帮助都值得期待.

谢谢!!

bdo*_*han 17

UPDATE

跟进这个:我可以这样做吗?如果xml返回为4 .....如果我正在构建一个Person对象,我相信这会扼杀.我可以只绑定我想要的xml元素吗?如果是,我怎么能这样做.

您可以按如下方式映射此XML:

input.xml中

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
    <NumberOfPersons>2</NumberOfPersons>
        <Person>
            <Name>Jane</Name>
            <Age>40</Age>
        </Person>
        <Person>
            <Name>John</Name>
            <Age>50</Age>
        </Person>
</Persons> 
Run Code Online (Sandbox Code Playgroud)

package forum7177628;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Persons")
@XmlAccessorType(XmlAccessType.FIELD)
public class Persons {

    @XmlElement(name="Person")
    private List<Person> people;

}
Run Code Online (Sandbox Code Playgroud)

package forum7177628;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlElement(name="Name")
    private String name;

    @XmlElement(name="Age")
    private int age;

}
Run Code Online (Sandbox Code Playgroud)

演示

package forum7177628;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Persons.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Persons persons = (Persons) unmarshaller.unmarshal(new File("src/forum7177628/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(persons, System.out);
    }

}
Run Code Online (Sandbox Code Playgroud)

产量

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Persons>
    <Person>
        <Name>Jane</Name>
        <Age>40</Age>
    </Person>
    <Person>
        <Name>John</Name>
        <Age>50</Age>
    </Person>
</Persons>
Run Code Online (Sandbox Code Playgroud)

原始答案

下面是使用Java SE API(包括JAXB)调用RESTful服务的示例:

String uri =
    "http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
    (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");

JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
    (Customer) jc.createUnmarshaller().unmarshal(xml);

connection.disconnect();
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息:


Kow*_*ser 7

JAX-RS是用于restful webservice的Java api.泽西岛是sun/oracle的一个实现.

你需要jaxb将xml转换为POJO.但并非总是如此,转换后的对象可以在没有任何转换的情况下使用.如果是这种情况,SAXParser是一个很好的解决方案.

是一个很好的JAXB教程.