Develop Microservice with JAX-RS

Concept Description

ServiceComb supports developers in developing services in JAX-RS mode by using JAX-RS.

Development Example

  • Step 1 Import dependencies into your maven project:

xml <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.servicecomb</groupId> <artifactId>java-chassis-dependencies</artifactId> <version>1.0.0-m1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--transport can optional import through endpoint setting in microservice.yaml, we import both rest and highway as example--> <dependency> <groupId>org.apache.servicecomb</groupId> <artifactId>transport-rest-vertx</artifactId> </dependency> <dependency> <groupId>org.apache.servicecomb</groupId> <artifactId>transport-highway</artifactId> </dependency> <dependency> <groupId>org.apache.servicecomb</groupId> <artifactId>provider-jaxrs</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> </dependencies>

  • Step 2 Implement the service. JAX-RS is used to describe the development of service code. The implementation of the Hello service is as follows:

```java import; import; import; import; import org.apache.servicecomb.samples.common.schema.models.Person;

@Path("/jaxrshello") @Produces(MediaType.APPLICATION_JSON) public class JaxrsHelloImpl { @Path("/sayhi") @POST public String sayHi(String name) {  return "Hello " + name; }

 public String sayHello(Person person) {
   return "Hello person " + person.getName();

} ```


In this sample the Path of sayHi is /jaxrshello/sayhi, and the Path of sayHello is /jaxrshello/sayhello, if you wish them /sayhi and /sayhello, please change the setting of @Path on the JaxrsHelloImpl to @Path("/").

  • Step 3 Release the service. Add @RestSchema as the annotation of the service implementation class and specify schemaID, which indicates that the implementation is released as a schema of the current microservice. The code is as follows:

java import; // other code omitted @RestSchema(schemaId = "jaxrsHello") public class JaxrsHelloImpl implements Hello { // other code omitted }

Create the jaxrsHello.bean.xml file in the resources/META-INF/spring directory and configure base-package that performs scanning. The content of the file is as follows:

```xml <?xml version="1.0" encoding="UTF-8"?>

   <context:component-scan base-package="org.apache.servicecomb.samples.jaxrs.provider"/>


  • Step 4 Add service definition file:

Add microservice.yaml file into resources folder of your project.

  • Step 5 Add Main class:

```java import; import;

public class Application { public static void main(String[] args) throws Exception { //initializing log, loading bean(including its parameters), and registering service, more detail can be found here : Log4jUtils.init(); BeanUtils.init(); } } ```

Involved APIs

Currently, the JAX-RS development mode supports the following annotation. For details about how to use JAX-RS, see JAX-RS official documentation

Remarks Location Description schema/operation URL path schema/operation Coding/decoding capability supported by the method operation http method operation http method operation http method operation http method parameter Obtain parameters from query string parameter Obtain parameters from path, This parameter must be defined in path. parameter Obtain parameters from header. parameter Obtain parameters from cookie.

NOTE: - When the method parameter has no annotation and is not an HttpServletRequest parameter, the parameter is of the body type by default. One method supports a maximum of one body-typed parameter. - You are advised to explicitly define the value of the parameter. Otherwise, the parameter name in the API definition is used, such as @QueryParam\("name"\) String name String name instead of @QueryParam String name.