IntelliJ idea + Maven + Tomcat + jersey build RESTful services

Source: Internet
Author: User
Tags apache tomcat

This article refers to the following:

[1] Starting out with Jersey & Apache Tomcat using IntelliJ

[2] "Jersey" IntelliJ idea + Maven + Jetty + Jersey build RESTful services

Thanks to the two authors.

0. Create a new project

Create a new project in IntelliJ, select the Java Enterprise, RESTful Web Service, Setup Libery later.

1. Join the web framework and the MAVEN framework

Right-click the project name, ADD frameworks support, and tick Web application and maven, respectively.

3. Adding jersey dependencies to Maven

Add in Pom.xml:

1 <Dependencies>2     <Dependency>3         <groupId>Com.sun.jersey</groupId>4         <Artifactid>Jersey-bundle</Artifactid>5         <version>1.19.1</version>6     </Dependency>7     <Dependency>8         <groupId>Com.sun.jersey</groupId>9         <Artifactid>Jersey-json</Artifactid>Ten         <version>1.19</version> One     </Dependency> A     <Dependency> -         <groupId>Com.sun.jersey</groupId> -         <Artifactid>Jersey-servlet</Artifactid> the         <version>1.19.1</version> -     </Dependency> -  </Dependencies>

At this point, the entire Pom.xml document is as follows. By the way, the MAVEN default Java source value, the target value version is 1.5, can be modified to 1.8, the method is shown in the code below.

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Projectxmlns= "http://maven.apache.org/POM/4.0.0"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">5     <modelversion>4.0.0</modelversion>6 7     <groupId>GroupId</groupId>8     <Artifactid>Test</Artifactid>9     <version>1.0-snapshot</version>Ten  One     <Properties> A         <Maven.compiler.source>1.8</Maven.compiler.source> -         <Maven.compiler.target>1.8</Maven.compiler.target> -     </Properties> the  -     <Dependencies> -         <Dependency> -             <groupId>Com.sun.jersey</groupId> +             <Artifactid>Jersey-bundle</Artifactid> -             <version>1.19.1</version> +         </Dependency> A         <Dependency> at             <groupId>Com.sun.jersey</groupId> -             <Artifactid>Jersey-json</Artifactid> -             <version>1.19</version> -         </Dependency> -         <Dependency> -             <groupId>Com.sun.jersey</groupId> in             <Artifactid>Jersey-servlet</Artifactid> -             <version>1.19.1</version> to         </Dependency> +     </Dependencies> - </Project>
View Code4. Create a source file

To create a new package under the Src/java directory, such as Com.test.jersey, create a new class Helloworld.java under the package and write the code:

 PackageCom.test.jersey;ImportJavax.ws.rs.GET;ImportJavax.ws.rs.Path;Importjavax.ws.rs.Produces; @Path ("/hello") Public classHelloWorld {//get annotation Set accept request type is get@GET//produces indicates that the data type sent out is Text/plain//corresponding to produces is @consumes, which indicates that the accepted data type is Text/plain@Produces ("Text/plain")     PublicString getMessage () {return"Hello world!"; }}
5. Configuring the Servlet

Edit Web/web-inf/web.xml, add code:

1 <Display-name>Test</Display-name>2 3     <servlet>4         <Servlet-name>Example API</Servlet-name>5         <Servlet-class>Com.sun.jersey.spi.container.servlet.ServletContainer</Servlet-class>6 7         <Init-param>8             <Param-name>Com.sun.jersey.config.property.packages</Param-name>9             <Param-value>Com.test.jersey</Param-value>Ten         </Init-param> One  A         <Init-param> -             <Param-name>Com.sun.jersey.api.json.POJOMappingFeature</Param-name> -             <Param-value>True</Param-value> the         </Init-param> -     </servlet> -  -     <servlet-mapping> +         <Servlet-name>Example API</Servlet-name> -         <Url-pattern>/*</Url-pattern> +     </servlet-mapping>
6. Configure Tomcat

Click Run >Edit configurations ... > "+" > tomcat Server > Local, join Tomcat, select Deployment tab, click the c11> "+", select the unique Artifact, click "OK".

7. Add a library file to the output

Select Project Structure, click Artifacts, you can right available elements there are many library files that are not included in the output. Double-click each file in turn.

8. Run Tomcat

Run Tomcat and enter Http://localhost:8080/hello in the browser to see the following output:

IntelliJ idea + Maven + Tomcat + jersey build RESTful services

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.