VRS rule engine WebService build and call <1>

Environment Construction

First , download CXF, the official website (http://cxf.apache.org/), the specific location is as follows:



After decompression, get the following directory, as shown in



the figure: Unzip the ..\apache-cxf in the above picture - All files in the 2.7.6\lib directory should be copied to the lib directory of the new project, especially the endorsed folder.

Service Construction of WebService

Create a project
Create a web project webService_cxf in eclipse/myEclipse, and click the menu item "File" - "New" - "Web Service Project" to create it, as shown in the figure below:



a pop-up form, the project name is webService_cxf, as shown below :



New interface file TypeService.java
package com.flageader.service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
//Must have @WebService
@WebService
public interface TypeService {
//basic type
public int testInt(short short1, short short2);
//array
public String testArray(int[] int1,char[] char1, byte[] byte1) ;
//集合 list
public List testList(List list) ;
//集合 list<String>
public List<String> testListString(String[] str1) ;
//集合 map
public Map testMap(int id, String name, int age, char sex, String grade,String remark) ;
//日期
public Date testDate(Date date) ;
}


新建实现类TypeServiceImpl.java
package com.flageader.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TypeServiceImpl implements TypeService {
/**
* 常见类型 int
*/
public int testInt(short short1, short short2) {
int int1 = short1 + short2;
return int1;
}
/**
* 数组
*/
public String testArray(int[] int1, char[] char1, byte[] byte1) {
String str = "";
str+="<int[]={";
for (int i = 0; i < int1.length; i++) {
str += int1[i] + ",";
}
str+="}>\n<char[]={";
for (int i = 0; i < char1.length; i++) {
str +=  char1[i] + ",";
}
str+="}>\n<byte[]={";
for (int i = 0; i < byte1.length; i++) {
str +=  byte1[i] + ",";
}
str+="}";
return str;
}
/**
* 集合 list
*/
public List testList(List list) {
return list;
}
/**
* 集合 list<String>
*/
public List<String> testListString(String[] str1) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < str1.length; i++) {
list.add(str1[i]);
}
return list;
}
/**
* 集合 map
*/
public Map testMap(int id, String name, int age, char sex, String grade,
String remark) {
Map map = new HashMap();
map.put("id", id);
map.put("name", name);
map.put("age", age);
map.put("sex", sex);
map.put("grade", grade);
map.put("remark", remark);
return map;
}
/**
* 时间
*/
public Date testDate(Date date) {
return date;
}
}


新建applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd"
default-lazy-init="false">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- implementor:为实现类的完整路径名 -->
<jaxws:endpoint id="typeService" implementor="com.flageader.service.TypeServiceImpl" address="/typeService" />
</beans>


Modify the configuration file web.xml
to find the web.xml file in the project, open it, replace the content in web.xml with the following content, and save it
<?xml version="1.0" encoding="UTF-8"?>
< web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener> <listener-class>org .springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
       <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:  applicationContext.xml</param-value> 
  </context-param> 
<servlet>





















Click the project "webService_cxf"-"New"-"Source Folder", as shown in the figure below:



Name it test, as shown in the figure below:



Click on the implementation class "TypeServiceImpl"-"New"-"Other...", as shown in the figure below:



Pop up Form, enter "test" in the text box in the form, and click "Junit Test Case", as shown in the figure below:



The form "Junit Test Case" will pop up, click "Browse...", and the form "Source Folder" will pop up. Selection", select "webService_cxf" - "Test", as shown in the figure below:



Click Next, as shown in the figure below:



Select the method, as shown in the figure below:



This creates the test class TypeServiceImplTest.java.

Add test class TypeServiceImplTest.java
package com.flageader.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java .util.Map;
import junit.framework.TestCase;
public class TypeServiceImplTest extends TestCase {
public void testTestInt() {
TypeService test=new TypeServiceImpl();
short short1=12;
short short2=24;
System.out.println("----int------\n"+test.testInt(short1, short2));
}
public void testTestArray() {
TypeService test=new TypeServiceImpl();
int[] int1={1,3,5,7};
char[] char1={'男','女','是','否'};
byte[] byte1={0,1};
System.out.println("----数组------\n"+test.testArray(int1,char1, byte1));
}
public void testTestList() {
TypeService test=new TypeServiceImpl();
List list=new ArrayList();
list.add("ddd");
list.add(3.4);
List list1=test.testList(list);
System.out.println("----list------");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public void testTestListString() {
TypeService test=new TypeServiceImpl();
String[] strs={"张三","李四","王五","赵六"};
List<String> list=test.testListString(strs);
System.out.println("----list<String>------\n");
for (int i = 0; i < list.size(); i++) { System.out.println("----list<String>------\n"+list.get(i));
}
}
public void testTestMap() {
TypeService test=new TypeServiceImpl();
Map map=test.testMap(1, "Li Li", 12, 'male', "6th grade", "none");
System.out.println("----map------\n"+map);
}
public void testTestDate() {
try {
TypeService test=new TypeServiceImpl();
SimpleDateFormat sim=new SimpleDateFormat("yyyy -MM-dd");
Date date =test.testDate(sim.parse("2004-09-02"));
System.out.println("----date------\n"+ date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}


Test result
The test result is as shown in the printout below:



Project release
myEclispe/Eclispe has already configured Tomcat (if not, please configure it), click " Tomcat 6.x" - "Add Deployment...", as shown in the figure below:



a pop-up window will pop up, select the project "webService_cxf", as shown in the figure below:



This will release the "webService_cxf" project.

start tomcat





Access path: http://localhost/webService_cxf/webServices/typeService?wsdl a) How to know the
access path
 localhost— ip address
//localhost:8080          webService_cxf
project name Refer to: ?wsdl b) Visit http://localhost/webService_cxf/webServices/typeService?wsdl to complete.



















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326828678&siteId=291194637
VRS