1

I have written a resteasy-spring service :

    @Controller
    @RequestMapping("/abr/asd")
    @Path("/ab/ac")
    public class JobSchedulerService {

        private static final Logger LOGGER = Logger.getLogger(JobSchedulerService.class);

        @GET
        @Path("/abc/{param}")
        public String scheduleJobs(@PathParam("param") String name) {
            return "Success";
        }
    }

When i try calling this service from Browser, the response is proper :

http://localhost:8080/ControlAppWeb/rest/ab/ac/abc/name
  • Success

But when i try calling it from a Rest Client API, it returns a web page which is actually the welcome page for the web application where i have incoroporated the rest service :

         try {
         URL url = new URL("http://localhost:8080/WebApp/rest/ab/ac/abc/name");
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();

         conn.setRequestMethod("GET");
         conn.setRequestProperty("Accept", "application/json");

         if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
         throw new RuntimeException("Failed! " + conn.getResponseCode());
         }
         BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String output;

         System.out.println("Output from Server is: ");
         while ((output = br.readLine()) != null) {
         System.out.println(output);
         }
         conn.disconnect();     
         } catch (MalformedURLException e) {        
         e.printStackTrace();
         } catch (IOException e) {      
         e.printStackTrace();       
         }

Kindly help, as to where am i going wrong in invoking from Java API

Further: i tries removing the controller and request mapping annotation from the Service, still the browser call worked and java rest client didn't. Weird thing is that even if my modify the url to anything random (in the java client) excluding the intial part (http://localhost:8080/ControlAppWeb), the output remains the same and never is an error thrown...

  • check your cookies in browser, probably you have already logged in so browser request is accepted normally, while your url connection is not authenticated yet – hoaz Nov 16 '12 at 14:39
  • @hoaz thanks for the suggestions...by login page i mean the welcome page (srry for the mixup)...but the problem still remains...also i tried removing the Controller and RequestMapping annotation from the service side but to no avail...also another thing noticed was when calling from java rest client, no matter what url i gave, except for the intial part (http://localhost:8080/ControlAppWeb)...the service output was always the same, i.e the web page...never did an error throw up...kindly suggest... – Saurabh Mishra Nov 18 '12 at 12:13

1 Answers1

0

The problem in this case seemed to be arising due to a 'filter' configured in the web.xml of the control application. Due to which all the requests to /* were getting redirected to the welcome page. Removing the filter configuration from the web.xml file resolved the problem and the response for the Java Rest API was 'Success' same as for the call from the Browser. Though i am still unclear on why the filter did not affect the request from browser but did so for the request from Java Client. Anyways hope this helps others who might be struggling with a similar problem.