Jersey creates standalone server

Source: Internet
Author: User

First, modify pom. xml:

[Plain]
<Dependency>
<GroupId> com. sun. jersey </groupId>
<ArtifactId> jersey-server </artifactId>
<Version >$ {jersey-version} </version>
</Dependency>
<Dependency>
<GroupId> com. sun. jersey </groupId>
<ArtifactId> jersey-grizzly2 </artifactId>
<Version >$ {jersey-version} </version>
<Scope> compile </scope>
</Dependency>

[Plain]
<Properties>
<Jersey-version> 1.15 </jersey-version>
</Properties>

Second, the Main. java imported package is as follows:
[Plain]
Import com. sun. jersey. api. container. grizzly2.GrizzlyServerFactory;
Import com. sun. jersey. api. core. PackagesResourceConfig;
Import com. sun. jersey. api. core. ResourceConfig;
Import java. io. IOException;
Import java.net. URI;
Import javax. ws. rs. core. UriBuilder;
Import org. glassfish. grizzly. http. server. HttpServer;
The code must be modified as follows:
[Plain]
Public class Main {
 
Private static int getPort (int defaultPort ){
// Grab port from environment, otherwise fall back to default port 9998
String httpPort = System. getProperty ("jersey. test. port ");
If (null! = HttpPort ){
Try {www.2cto.com
Return Integer. parseInt (httpPort );
} Catch (NumberFormatException e ){
}
}
Return defaultPort;
}
 
Private static URI getBaseURI (){
Return UriBuilder. fromUri ("http: // localhost/"). port (getPort (9998). build ();
}
 
Public static final URI BASE_URI = getBaseURI ();

Protected static HttpServer startServer () throws IOException {
System. out. println ("Starting grizzly ...");
ResourceConfig rc = new PackagesResourceConfig ("com. esri ");
Return GrizzlyServerFactory. createHttpServer (BASE_URI, rc );
/*
Final Map <String, String> initParams = new HashMap <String, String> ();
 
InitParams. put ("com. sun. jersey. config. property. packages", "com. esri ");
 
System. out. println ("Starting grizzly2 ...");
Return GrizzlyWebContainerFactory. create (BASE_URI, initParams );
*/
}

Public static void main (String [] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = startServer ();
System. out. println (String. format ("Jersey app started with WADL available"
+ "% Sapplication. wadl \ nHit enter to stop it ...",
BASE_URI ));
System. in. read ();
HttpServer. stop ();
}
}

The comments in the startServer function are the old code generated by the wizard.
Okay. Start it again. Everything is normal.

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.