Chapter 4: Building jersey-based RESTful api with IntelliJ IDEA

Keywords: Java Maven xml Tomcat Apache

Compiler: Intellij IDEA
System environment: MAC OS
Related technologies: Maven, tomcat 7, jdk8

1. Create projects

First, create a web Application project (we're going to use maven to introduce Jersey's related jar package, so we don't need to create a restful project directly, because the restful project will download Jersey related jar package to the project lib folder)

After the project is created, we use Add Frameworks Support to introduce support for the maven project.

So far, we've successfully created a new web project and can happily transform its coding

2. Key Codes

2.1 Environment Configuration

Modify pom.xml to introduce Jersey-related jar packages

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>pers.marscheng.restful</groupId>
    <artifactId>restfulDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22.2</version>
        </dependency>
    </dependencies>

</project>

Modify web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <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>pers.marscheng.restful</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>
</web-app>

Where pers.marscheng.restful corresponds to the package where the restful demo code is placed, / api / corresponds to the address of the restful api mapping. Normally we don't set this address as /*, because this overrides the default index address unless you redefine the home address yourself.

2.2 Execution Code

After configuring the environment, we can write a simple restful api. Create a new Hello.java file under pers.marscheng.restful.

package pers.marscheng.restful;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * restful Test class
 *
 * @author marscheng
 * @create 2017-07-26 3:19 p.m.
 */
@Path("hello")
public class Hello {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello(){
        return "Hello,I am text!";
    }

    
    @POST
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello,I am xml!" + "</hello>";
    }
    
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>" + "Hello,I am html!" + "</body></h1>" + "</html> ";
    }


}
  1. PATH corresponds to the subchannel strength of the restful api we are configuring. For example, if we configure / API /*, the path for users to access the API is HTTPS /: ip: port / API / hello.
  2. GET@POST corresponds to the HTTP method used by users to request resources
  3. Produces represents the data type returned, such as MediaType.TEXT_PLAIN corresponding to the text type returned

3. Start the project

Put the jar package introduced by maven under the lib folder (if you type the project into war package directly through maven and put it under Tomcat, this step can be avoided)

Introduce the project in tomcat and start it

get requests with web pages

Direct get requests with postman

post requests with postman

Posted by cairesdesigns on Sat, 12 Jan 2019 16:48:10 -0800