从Java调用Restful Service [英] calling Restful Service from Java

查看:152
本文介绍了从Java调用Restful Service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here I am not creating a RESTful service indeed I have to call an external Restful service from my java code. Currently I am implementing this using Apache HttpClient. The response that I get from the web service is in XML format. I need to extract the data from XML and put them on Java objects. Rather than using SAX parser, I heard that we can use JAX-RS and JERSEY which automatically maps the xml tags to corresponding java objects.

我一直在浏览,但无法找到开始使用的资源.我确实查看了现有链接使用Java消费RESTful API Java中的RESTful调用

I have being looking through but unable to find a source to get started. I did look at existing links Consuming RESTful APIs using Java RESTful call in Java

在前进的过程中,我们将提供任何帮助.

Any help is appreciated in moving forward.

谢谢!

推荐答案

更新

作为后续操作:我可以这样做吗?如果返回xml如4.....如果我要构造一个Person对象,我相信这会阻塞.我可以只绑定我想要的xml元素吗?如果可以,我该怎么办那个.

as follow up with this: Can I do this way?? if the xml being returned as 4 ..... If I am constructing a Person object, I believe this will choke up. Can I just bind only the xml elements that I want? if Yes how can I do that.

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

You could map this XML as follows:

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> 

人员

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;

}

人员

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;

}

演示

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

}

输出

<?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>


原始答案

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

Below is an example of calling a RESTful service using the Java SE APIs including JAXB:

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

有关更多信息:

这篇关于从Java调用Restful Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆