揭露现有的API作为一个Web服务 [英] Exposing existing API as a Web service

查看:125
本文介绍了揭露现有的API作为一个Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个任务,露出一个API作为Web服务。这里的想法是在JAR文件中的现有业务逻辑打包成一个WAR文件,揭露WAR文件作为Web服务,它会返回一个自由形式的XML字符串。当我们揭露现有的API作为一个Web服务,它是足够的,我们可以做一个XSD和放大器;返回的XML字符串数据的WSDL文件?是该公约或标准的做法?

I am currently working on a task to expose an API as a Web service. The idea here is to package the existing business logic in JAR files into a WAR file and expose the WAR file as a Web service that would return a free-form XML string. When we expose an existing API as a Web service, is it sufficient that we make available an XSD & WSDL file of the returned XML string data? Is that the convention or the standard practice?

推荐答案

这取决于您是否正在使用SOAP或REST。 SOAP是更多的限制;因此,它更期望你有一个WSDL文件生成的API接口的类。

It depends on whether or not you are using SOAP or REST. SOAP is more restrictive; as a result, it's more expected that you'll have a WSDL file to generate the classes that interface with the API.

在另一方面,如果你正在使用REST,只露出一个RESTful URI将被视为足以满足具有均匀接口RESTful Web服务的限制。

On the other hand, if you are using REST, just exposing a RESTful URI would be considered enough to meet the constraint of a RESTful web service having a uniform interface.

REST往往通过SOAP中的应用越来越普及,因为它是一个宽容的建筑风格。我想preFER这种方法,如果你是新来开发Web服务我会推荐这种方法。

REST tends to be gaining more ground over SOAP since it is a permissive architectural style. I would prefer this method, and I would recommend this method if you're new to developing web services.

根据您正在使用,我假设的Java什么语言,你可以使用Restlets或Spring 3.0的REST框架来帮助你建立一个RESTful Web服务。这些工具真正使这项工作轻松了很多,并帮助你符合的6约束基于REST的Web服务并符合<一个href=\"http://en.wikipedia.org/w/index.php?title=Re$p$psentational_state_transfer&oldid=582593205#Key_goals\"相对=nofollow> 4关键目标的。

Depending on what language you are using, I'm assuming Java, you can use Restlets or Spring 3.0's REST framework to help you build a RESTful web service. These tools really make that job a lot easier and help you conform to the 6 Constraints of a RESTful Web Service and meet the 4 Key Goals.

更新:

假设你已经有现有的,面向对象的code,假设你要揭露了code作为一个REST API,使用Spring 3.0 MVC,创建一个控制器子类,将包装现有的绕包:

Assuming you already have existing, object-oriented code, and assuming you want to expose that code as a REST API, using Spring 3.0 MVC, create a Controller subclass that will wrap around your existing package:

示例GET

资源:杰克逊的ObjectMapper POJO / JSON的Marshaller 的Javadoc

Resource: Javadocs for Jackson's ObjectMapper POJO/JSON Marshaller

 // this is the wrapper around your existing Java packages.  
 @Controller
 public class UserController {

     protected static final DATA_TYPE = "json";

     // In REST, GET method is used to retrieve data with no side effects, 
         // meaning that no changes are made to the data on the server.
     @RequestMapping(value="/users/{username}", method=RequestMethod.GET)
     public void getUserData(@PathVariable("username") String userName, Model model) {

         // this is your existing class
         UserDataService userDataService = new UserDataService();

         // assume you have a class User, and getUserDetails gives you that POJO object.
         User user = userDataService.getUserDetails(username);


         // marshal the User object to JSON, using Jackson, and write as output in response
         ObjectMapper mapper = new ObjectMapper();
         mapper.writeValue(response.getWriter(), user);

     }
 }

 // assume you have an existing POJO class called User
 class User implements Serializable {

     String username;
     String age;
     String birthday;
     String mood;

     String getMood() { return this.mood; }
     String getBirthday() { return this.birthday; }
     String getAge() { return this.age; }
     String getUsername() { return this.username; }

     String setMood(String mood) { this.mood = mood; }
     String setBirthday(String birthday) { this.birthday = birthday; }
     String setAge(String age) { this.age = age; }
     String setUsername(String username) { this.username = username; }
}

请求

http://api.example.com:8080/users/jmort253/

响应:

{ 
    "username":"jmort253",
    "mood":"good",
    "age":"not too old and not too young",
    "birthday","Jan 1, 1900"
}

XML而不是JSON

返回XML和返回JSON之间的主要区别是所用的编组。使用javax.xml.bind.annotations,您可以将注释的POJO类,以便编组可以将其转换为XML,从有细节释放您手动手动code XML:

The main difference between returning XML and returning JSON is in the marshaller used. Using javax.xml.bind.annotations, you can place annotations on the POJO class so the marshaller can convert it to XML, freeing you up from the details of having to manually code XML by hand:

<一个href=\"http://technology.amis.nl/blog/2046/using-the-javaxxmlbind-annotations-to-convert-java-objects-to-xml-and-xsd\"相对=nofollow>使用javax.xml.bind.annotations到Java转换对象,XML和XSD 。这种资源还解释了如何生成XML模式,如果你认为这是你的REST Web服务的要求。

Using javax.xml.bind.annotations to convert Java Objects to XML and XSD. This resource also explains how to generate the XML Schema, if you deem that as a requirement to your REST Web service.

 @XmlRootElement
 class User implements Serializable {

     String username;
     String age;
     String birthday;
     String mood;

     String getMood() { return this.mood; }
     String getBirthday() { return this.birthday; }
     String getAge() { return this.age; }
     String getUsername() { return this.username; }

     String setMood(String mood) { this.mood = mood; }
     String setBirthday(String birthday) { this.birthday = birthday; }
     String setAge(String age) { this.age = age; }
     String setUsername(String username) { this.username = username; }
}

而不是使用API​​杰克逊的ObjectMapper级名帅的POJO类JSON,到位ObjectMapper的使用javax.xml.bind.annotations包:

Instead of using the Jackson API's ObjectMapper class to marshal the POJO class to JSON, use the javax.xml.bind.annotations package in place of ObjectMapper:

JAXBContext context = JAXBContext.newInstance(User.class);
Marshaller marshaller = context.createMarshaller();

// pretty print XML
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(user, System.out);

除了其他资源,本文使用的 JAXB反序列化POJO的ArrayList对象到XML

我对REST Web服务的封装工作时最后一个建议是设置日志记录级别为ALL或调试。我觉得这可以帮助我更轻松地确定的建立Web服务时,我面对任何问题的根源。库本身将输出有用的调试信息,以帮助您解决配置问题,如缺少的依赖,缺少注释,以及与转换为XML / JSON过程中或在配置Spring 3.0打交道时,你可能会遇到的其它问题。

My final suggestion when working on the REST Web service wrappers is to set your logging levels to "ALL" or "DEBUG". I find that this helps me more easily determine the root cause of any problems I face when setting up a Web service. The libraries themselves will output helpful debug messages to help you resolve configuration issues, such as missing dependencies, missing annotations, and other issues that you'll likely encounter when dealing with the conversion to XML/JSON process or in setting up Spring 3.0.

一旦你统一的接口设置,可以使GET请求和接收响应,然后就可以设置日志记录级别回到previous INFO或WARN水平。

Once you're uniform interfaces are setup and you can make GET requests and receive responses, you can then set the logging levels back to the previous INFO or WARN levels.

这篇关于揭露现有的API作为一个Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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