使用CXF和Jersey框架进行Java的WebService编程步骤如下:
-
配置pom.xml文件,添加CXF和Jersey框架相关的依赖。
```
org.apache.cxf
cxf-bundle-jaxrs
3.3.6
org.glassfish.jersey.core
jersey-server
2.30
org.glassfish.jersey.containers
jersey-container-servlet-core
2.30
``` -
创建实现WebService接口的类和类的实现方法。其中,方法需要用
@GET
或@POST
等注解标记,并且也需要注解类@Path
指定URL路径。```
@Path("/helloworld")
public class HelloWorldServiceImpl implements HelloWorldService {@Override @GET @Path("/sayHello") public String sayHello() { return "Hello World"; }
}
``` -
在web.xml文件中配置CXF和Jersey框架相关的servlet和filter。
```
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
javax.ws.rs.Application com.example.ws.HelloWorldApplication
CXFServlet
/services/*
JerseyServlet
org.glassfish.jersey.servlet.ServletContainer
javax.ws.rs.Application com.example.ws.HelloWorldApplication
JerseyServlet
/jersey/*
CXFFilter
org.apache.cxf.jaxrs.servlet.CXFServletFilter
CXFFilter
/services/*
JerseyFilter
org.glassfish.jersey.servlet.ServletContainer
javax.ws.rs.Application com.example.ws.HelloWorldApplication
JerseyFilter
/jersey/*
``` -
创建继承
javax.ws.rs.core.Application
的类,利用register
方法注册WebService实现类并指定URL路径。```
public class HelloWorldApplication extends Application {@Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<Class<?>>(); classes.add(HelloWorldServiceImpl.class); return classes; } @Override public Set<Object> getSingletons() { Set<Object> singletons = new HashSet<Object>(); singletons.add(new HelloWorldServiceImpl()); return singletons; }
}
``` -
启动Tomcat服务器,在浏览器中输入以下URL地址,查看WebService是否成功访问。其中,
localhost:8080
表示Tomcat的访问地址,/services/helloworld/sayHello
和/jersey/helloworld/sayHello
表示不同的WebService访问路径。```
http://localhost:8080/services/helloworld/sayHellohttp://localhost:8080/jersey/helloworld/sayHello
```
示例1:利用CXF和Jersey框架实现计算器WebService
-
创建
CalculatorService
接口。```
@Path("/calculator")
public interface CalculatorService {@GET @Path("/add/{num1}/{num2}") public int add(@PathParam("num1") int num1, @PathParam("num2") int num2); @GET @Path("/subtract/{num1}/{num2}") public int subtract(@PathParam("num1") int num1, @PathParam("num2") int num2);
}
``` -
创建
CalculatorServiceImpl
实现类。```
public class CalculatorServiceImpl implements CalculatorService {@Override public int add(int num1, int num2) { return num1 + num2; } @Override public int subtract(int num1, int num2) { return num1 - num2; }
}
``` -
在
HelloWorldApplication
中注册CalculatorServiceImpl
类。Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(CalculatorServiceImpl.class); -
启动Tomcat服务器,在浏览器中输入以下URL地址,进行计算器WebService访问。
```
http://localhost:8080/services/calculator/add/1/2http://localhost:8080/jersey/calculator/subtract/3/2
```
示例2:利用CXF和Jersey框架实现文件上传WebService
-
在
HelloWorldApplication
中注册FileUploadService
类。Set<Object> singletons = new HashSet<Object>();
singletons.add(new FileUploadService()); -
创建
FileUploadService
类,并在类中创建文件上传方法。```
@Path("/fileupload")
public class FileUploadService {@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) { String uploadedFileLocation = "C://temp/" + fileDetail.getFileName(); try { OutputStream out = new FileOutputStream(new File(uploadedFileLocation)); byte[] buffer = new byte[1024]; int bytesread = 0; while ((bytesread = uploadedInputStream.read(buffer)) != -1) { out.write(buffer, 0, bytesread); } out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return Response.status(200).entity("File uploaded to : " + uploadedFileLocation).build(); }
}
``` -
启动Tomcat服务器,在浏览器中输入以下URL地址,进行文件上传WebService访问。其中,
file
表示文件参数的名字。```
POST http://localhost:8080/services/fileupload/upload
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="example.txt"[binary data]
```
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用CXF和Jersey框架来进行Java的WebService编程 - Python技术站