0

I am trying to do something like the below using jersey :

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;


public class route {
    private final Logger logger = LogManager.getLogger(route.class);
    private ResourceBundle errMsgs = ResourceBundle.getBundle(Master.ROUTE.getBundleName());

    @GET @Path("route/{v_route}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response response(@PathParam("v_route") String vp_route) {
        String[] strings = {vp_route};
        if (Misc.isEmptyNull(strings)) {
            logger.error(errMsgs.getString("missingRouteID"));
            throw new WebException(errMsgs.getString("missingRouteID"));
        }
        return Response.ok("ok",MediaType.TEXT_PLAIN).build();
    }

    @POST @Path("route")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response postResponse(@FormParam("v_auditid") String p_auditid,
                                 @FormParam("v_tenant") String p_tenant){
   // removed to keep short 
   }

However, even a simple cURL get to the basic http://site/WebApi/route/parameter yields the following :

"" - Error report

HTTP Status 404 - Not Found


type Status report

messageNot Found

descriptionThe requested resource is not available.


""

Two important things to note before you rush to answer this question :

  1. Nothing appears in /opt/glassfish4/glassfish/domains/mydomain/logs/server.log
  2. Yes I am sure http://site/WebApi is the correct prefix. I have another jersey java class that works perfectly fine under the same prefix !
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Little Code
  • 1,315
  • 2
  • 16
  • 37

1 Answers1

2

See sections 4.2, 4.10, and 4.7.1.1 of the jersey documentation here https://jersey.java.net/documentation/latest/deployment.html you need to create an application extended class and add a scanner to the web.xml config for jersey to use your annotated class

See my blog with example

http://blog.vbranden.com/2015/03/creating-jersey-webservice-in-gatein.html

There are a few ways for jersey to use your annotated class. The example here uses resourceconfig as opposed to application. There is also a method for doing this completely in web.xml but application and resource config allow you more flexibility

example using application extended class

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>route</display-name>
    <servlet>
        <servlet-name>route</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.yourcompany.app.package.WSApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>route</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

application extended class WSApplication.java

package com.yourcompany.app.package;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import com.yourcompany.annotated.package.*;

public class WSApplication extends Application {

    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(route.class);
        return s;
    }
}

of course the package names should be altered to meaningful ones. and any other path annotated classes just get added to the HashSet as new items.

vbranden
  • 5,814
  • 2
  • 22
  • 15
  • (+1) but `POJOMappingFeature` is completely wrong. That is for Jersey 1.x There is no such property in Jersey 2.x. As a general rule, Jersey 2.x should not use anything `com.sun.jersey`. Also with the Jackson module you are using in your blog post, there is no extra configuration needed. – Paul Samsotha Apr 22 '15 at 23:21
  • Cool didn't know that but it makes sense. I will update answer and blog. Thanks! – vbranden Apr 22 '15 at 23:39