介绍通过 Intellij IDEA 来创建一个 RESETful API 项目,该项目适用于基于 JavaWEB 后端技术的项目,相关技术包含 Maven、Jersey、Hibernate 等,这里不对这些技术进行过多的详细介绍,只是创建一个简单的入门项目用于学习
Intellij IDEA 官网
附上破解教程 IntelliJ IDEA 2017.3.2永久破解版
10.* 9.*
这里就不介绍如何安装 JDK 和 Tomcat
Java JDK 将其配置成安装的版本,Tomcat 服务器也一样配置成最新的版本
点击 Next
进入一下,配置一下项目名
点击 Finish
完成,在项目名上右键 -> Add Frameworks Support 把 maven 项目的支持引入
将以下 XML
代码复制到项目的 pom.xml
文件中,注意Intellij IDEA 生成的 pom.xml
文件无须更改,只需要将以下代码复制到 project
标签以内即可
<properties> <maven.compiler.source>10.0.1</maven.compiler.source> <maven.compiler.target>10.0.1</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jersey.version>2.27</jersey.version> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.inject/jersey-hk2 --> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>${jersey.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-json --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.19.4</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.1</version> </dependency> <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.glassfish.hk2/hk2 --> <dependency> <groupId>org.glassfish.hk2</groupId> <artifactId>hk2</artifactId> <version>2.5.0-b61</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.3.3.Final</version> </dependency> </dependencies>
保存文件之后,会弹出以下提示界面,点击 Enable Auto-Import
即可将会为你自动下载依赖
点击右侧的 Maven Pjojects
检查依赖包是否全部下载完整,当然如果是第一次下载的话是需要些时间,耐心等待即可,如果长时间没有下载完整,则需手动点击 Reimport All Maven Pjojects
或者 Download Sourse and/or Documentation
,未完成下载表现为 pom.xml
中有红色字体出现,或者以下界面的依赖包会有红色的波浪线
按下快捷键 Ctrl++Alt+Shift+S
呼出项目配置,将 Maven 引入的 jar 包放到 lib 文件夹下
在 src/main/java
中创建包 com.API.actions
用来保存测试类文件
新建 Hello.java
,将以下代码复制进去即可
package com.API.actions; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("hello") public class Hello { @GET @Produces(MediaType.TEXT_PLAIN) public String sayHello() { return "Hello,I am text!"; } }
配置 web.xml
<servlet> <servlet-name>JAX-RS Servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.API.actions</param-value> <!-- 这里的配置包路经必须是与之间创立用于存储测试类的包路径一致 --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JAX-RS Servlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
点击启动 Tomcat 服务,稍等片刻
在浏览器中输入 http://127.0.0.1:8080/api/hello 即可看到结果
在 PostMan
里也可以获取结果
在 setting
中添加 JRebel 插件,这里已经安装过则显示 uninstall
附上 JRebel 破解通过包及其教程
JRebel.rar