Go to WebService universal interface and webservice universal interface

Source: Internet
Author: User

Go to WebService universal interface and webservice universal interface

The source code of webservice interfaces in many small and medium-sized projects can not help but have an impulse to speak. In addition to the CXF, Axis2, and other god-level frameworks, other lazy frameworks are Ctrl + V, Ctrl + C, and the requests and Response of other modules are pasted. Once a feature is added, you have to go through all requests and Response. Early in the morning, why.

The key to writing a good Weservice Interface is to make a summary. In addition to Request and Response, there are many more. Summary:

Request: webservice input.

Response: webservice output.

DataRsponse: webservice output with a result set.

ResultData: The ResultData of different interfaces in the result set.

ReslutInfo: the object that indicates the successful or failed call request.

WebServiceTemplate: A template class that defines how to call lower-level functions and how to handle exceptions and errors.

ResultDataCallback: callback class. It works with template and is applicable to the [return result set] operation, such as [getting user information] and [placing orders]

ResultCallback: callback class, with Template configuration, applicable to [interfaces with no result set returned, such as [modify user name]

ResponseFactory: The factory class used to assemble webservice messages.

ResultInfoFactory: The factory class used to handle exception information. This interface implements a default instance for it.

With these younger siblings, you can easily define any webservice interface. If you don't believe me, I will give you an example. Let's use [addition] as an example!

AddRequest:

public class AddRequest extends Request{        private int add;    private int added;        public int getAdd() {        return add;    }    public void setAdd(int add) {        this.add = add;    }    public int getAdded() {        return added;    }    public void setAdded(int added) {        this.added = added;    }    }

AddResonse:

public class AddResponse extends DataResponse<AddResultData>{    private AddResultData resultData;    public AddResultData getResultData() {        return resultData;    }    public void setResultData(AddResultData resultData) {        this.resultData = resultData;    }}

AddResultData:

public class AddResultData implements ResultData{    private int sum;    public AddResultData(int sum) {        super();        this.sum = sum;    }    public AddResultData() {        super();    }    public int getSum() {        return sum;    }    public void setSum(int sum) {        this.sum = sum;    }}

A simple webservice interface:

public class Soap {        public AddResponse add(final AddRequest request){        return WebServiceTemplate.doResult(new ResultDataCallback<AddRequest,AddResponse,AddResultData>(){            public AddResultData done() throws Exception {                return new AddResultData(request.getAdd()+request.getAdded());            }            public AddResponse getRsp() {                return new AddResponse();            }        });    }}

A simple TestCase test:

public class TestSoap {        @Test    public void testAdd(){        AddRequest add = new AddRequest();        add.setAdd(1);        add.setAdded(2);                Soap soap = new Soap();        AddResponse rsp =soap.add(add);        assertEquals(3,rsp.getResultData().getSum());            assertEquals("0",rsp.getResultInfo().getResultCode());        assertEquals("success",rsp.getResultInfo().getResultDesc());    }}

Although these are simple, they can solve 80% of problems. The remaining 20% depends on the actual situation. Besides, you still have room for continuous optimization of these codes. Never be a Copy-loving child.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.