JAX-RS 2.0 lifecycle support

About this task

In WebSphere® Application Server tradicional, only the JAX-RS root resource classes, providers, and application subclasses support lifecycle annotation.

The following example is based on the JAX-RS root resource classes. To add the lifecycle support on the JAX-RS root resource, proceed with the following steps:

Procedure

  1. Import both the PostConstruct annotation and the PreDestroy annotation to the BasicResource.java file according to the following code snippet:
    package com.ibm.ws.sample.pojo;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    
    @Path("BasicResource")
    public class BasicResource {
    
    	private final String prefix = "[Basic Resource]:";
    
    	@GET
    	@Path("echo/{param}")
    	public String echo(@PathParam("param") String param) {
    		System.out.println("echo method invoked!");
    		return prefix + param;
    	}
    
           @PostConstruct
    	public void echo2() {
    
    		System.out.println("PerRequest @PostConstruct invoked!");
    	}
    
    	@PreDestroy
    	public void echo3() {
    
    		System.out.println("PerRequest @PreDestroy invoked!");
    	}
    
    }
  2. Add the @PostConstruct and @PreDestroy annotations on the method according to your need. In the following example, when you call the per-request resource in this sample, you might see the following output:

    PerRequest @PostConstruct invoked!
    echo method invoked!
    PerRequest @PreDestroy invoked!


Ícone que indica o tipo de tópico Tópico de Tarefa



Ícone de registro de data e hora Última atualização: July 9, 2016 7:57
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=twbs_jaxrs2
Nome do arquivo: twbs_jaxrs2.0_lifecycle.html