如何使用Spring MVC使用REST URL? [英] How to consume REST URLs using Spring MVC?
问题描述
我已经开发了几种RESTful方法,并通过Apache Cxf公开了它们
I have developed few RESTful methods and exposed them via Apache Cxf
我正在使用Spring MVC开发客户端应用程序,并且正在寻找一个简单的示例来演示如何使用Spring MVC调用/使用这些REST方法
I'm developing the client side application using Spring MVC and I'm looking for a simple example to demonstrate how to call/consume these REST methods using Spring MVC
我知道如何使用Apache http客户端来做到这一点,但更喜欢使用Spring MVC,以防这种情况已在此处实现.
I know how to do it using Apache http client but prefer to use Spring MVC in case such this has already been implemented there.
推荐答案
Spring provides simple wrapper to consume RESTful services called RestTemplate
. It performs path variable resolution, marshalling and unmarshalling:
Map<String, Integer> vars = new HashMap<String, Integer>();
vars.put("hotelId", 42);
vars.put("roomId", 13);
Room room = restTemplate.getForObject(
"http://example.com/hotels/{hotelId}/rooms/{roomId}",
Room.class, vars);
假定Room
是一个RestTemplate
可以理解的JAXB对象.
Assuming Room
is a JAXB object which can be understood by The RestTemplate
.
请注意,此类与Spring MVC没有关系.您可以在MVC应用程序中使用它,也可以在独立应用程序中使用它.这是一个客户端库.
Note that this class has nothing to do with Spring MVC. You can use it in MVC application, but also in a standalone app. It is a client library.
这篇关于如何使用Spring MVC使用REST URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!