Maven + Jetty + jersey to build restful services

Source: Internet
Author: User
Tags apache tomcat

IntelliJ idea + Maven + Jetty + jersey build RESTful services

This article refers to the following:

Using jersey to achieve restful webservice (i)

Starting out with Jersey & Apache Tomcat using IntelliJ

--------------------------------------------------Body--------------------------------------------------------------

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

Second, after creating the project Jerseydemo, right-click on the project ADD frameworks support, check the Web application and maven respectively. Of these, web Appication adds Web.xml,maven as a build tool for the project.

The file structure of the project after completion is as follows:

Iii. adding jersey and jetty to the Pom.xml:

<dependencies>    <dependency>        <groupId>com.sun.jersey</groupId>        < artifactid>jersey-bundle</artifactid>        <version>1.19.1</version>    </dependency >    <dependency>        <groupId>org.mortbay.jetty</groupId>        <artifactid>jetty </artifactId>        <version>6.1.25</version>    </dependency>    <dependency>        <groupId>com.sun.jersey</groupId>        <artifactId>jersey-json</artifactId>        <version>1.19</version>    </dependency>    <dependency>        <groupid >com.sun.jersey</groupId>        <artifactId>jersey-servlet</artifactId>        <version >1.19.1</version>    </dependency> </dependencies>

IV. Create the package and class under src/main/java/, here I create a Hellojsersy class with the following code:

Package Com.puyangsky.example;import javax.ws.rs.*;
path annotation to set URL access path @path ("/hello") public class HelloWorld {
Get annotations Set accept request type to 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") public String getString () { return "Hello jersey!"; }}

Then use jetty to create a server class Startentity.java:

1 package com.puyangsky.example; 2 3 Import Com.sun.jersey.api.core.PackagesResourceConfig; 4 Import Com.sun.jersey.spi.container.servlet.ServletContainer; 5 Import Org.mortbay.jetty.Server; 6 Import Org.mortbay.jetty.servlet.Context; 7 Import Org.mortbay.jetty.servlet.ServletHolder; 8 9/**10 * Created by User01 on 2016/4/8.11 */12 public class Startentity {$ public static void main (string[] Arg          s) {+ Server server = new server (8090); Servletholder sh = new Servletholder (servletcontainer.class); 16 Sh.setinitparameter ("Com.sun.jersey.config.property.resourceConfigClass", PackagesResourceConfig.class.getCanonicalName ()); Sh.setinitparameter (" Com.sun.jersey.config.property.packages ","Com.puyangsky.example")://start server19 Context context = new context (server, NULL), Context.addservlet (SH, "/*");             try {server.start (); Server.join (); "Catch (Exception e) {25 E.printstacktrace (); 26}27 28}29}

The first of the red font is the port number, you can set it yourself, the second one needs to be modified by yourself, that is, the package name where the first Hellojersey.java is located.

OK, right click, Run "Startentity.main ()"

To access Http://localhost:8090/hello in the browser, or use the test RESTful Web Service in IntelliJ, the results are as follows:

Done!

------------------------------------------------------some tips------------------------------------------------

1, Intellj shortcut keys:

Artifact is the artifact, of course there are different places, such as we want to write a main method, do not enter a large string, as long as the input "PSVM", enter, done!

Similar to the output, just enter "Souf" and right-click. There are a lot of classes, to find out for themselves.

2, jetty occupy the port number is not released, each time a port number is very troublesome, then what should be done?

Because I did it on the Windows7, so win+r Open the DOS command line, enter Netstat-ano | Findstr "8090":

The last column is the process id,pid. So as long as the Kill is OK, and then enter: Taskkill/pid 12336/f

Results:

Here because 12236 has been hung, so changed a PID, the same effect.

More use of Jersey will continue to be covered in the next blog post.

Maven + Jetty + jersey to 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.