Đầu tiên ta tạo 1 maven project với type là maven-archetype-webapp hoặc có thể xem cách tạo maven project với eclipse . Ta cần phải cài đặt apache-maven, wildfly(ở đây xài wildfly 10)
Sau khi ta tạo xong ta vào file pom.xml thêm dependencies của javaee-api ta sẽ được như sau.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> ... <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- manage version lib --> <javaee.version>7.0</javaee.version> </properties> <dependencies> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>${javaee.version}</version> <scope>provided</scope> </dependency> </dependencies> ... </project>Tiếp đến ta cần tạo APIConfiguration class như sau
@ApplicationPath("api") public class APIConfiguration extends Application { }Sau khi đã tạo xong class api configuration ta tạo thêm class HelloworldAPI với nội dung như sau
@Path("hello") public class HelloworldAPI { @GET public String sayHello() { return "hello world"; } }
Tiếp đến ta dùng maven để build project(hoặc có thê xem cách dùng maven project ở đây) thành file helloworld.war.
Bước cuối cùng la sẽ deploy project lên một web server nào đó(ở đây mình xài wildfly)
Sau cùng ta vào trình duyệt gõ vào đường http://localhost:8080/helloworld/api/hello dẫn tới hello world ta sẽ được kết quả như mong muốn
Tải source code ở đây
https://drive.google.com/open?id=0B-JvPy4-xKqkUGsxOTZNalA4NW8
No comments:
Post a Comment