Tuesday, June 7, 2016

JAX-RS 2.0 Client API on Tomcat 8 running Spring 4, Hibernate 4 with a Jersey 2 RestController

Rest Client

package com.obrienlabs.gps.integration;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import com.obrienlabs.gps.business.entity.Record;
import com.obrienlabs.gps.business.entity.User;
public class JAXRSClient {
public void run() {
Long id = 1L;
Client client = ClientBuilder.newClient();
WebTarget rootTarget = client.target("http://127.0.0.1:8080/biometric/rest/read");
WebTarget latestTarget = rootTarget.path("json/latest");
WebTarget finalTarget = latestTarget.path(id.toString());
Record record = finalTarget.request().get(Record.class);
System.out.println(record);
client.close();

}
public static void main(String[] args) {
JAXRSClient client = new JAXRSClient();
client.run();
}

Rest Backend 


pom.xml


RestController

@Path("/read")
////@Produces("text/plain")
//@Consumes("application/json")
//@RestController
//@Service("daoFacade")
//@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@RequestMapping("/read")
@Component
public class RestServiceImpl extends Application {
// https://jersey.java.net/documentation/latest/spring.html
// http://stackoverflow.com/questions/3027834/inject-a-ejb-into-jax-rs-restfull-service
// not supported until 2.0 https://java.net/jira/browse/JERSEY-517
//@EJB(name="ejb/ApplicationService") // not for JAX-RS
// 20160606: spring injection requires jersey-spring3 from org.glassfish.jersey.ext with spring 4 exclusions in the pom
@Inject
//@Autowired
@Qualifier("daoFacade")

    private ApplicationServiceLocal applicationServiceLocal;// = new ApplicationService();

   @GET
   @Path("/json/record/{identifier}")  
   @Produces(MediaType.APPLICATION_JSON)
   public String getRecordJSON(@PathParam("identifier")String identifier) {    
      return Integer.toString(read(identifier).size());
   }

Total Pageviews

Followers