Search code examples
web-servicesrestspring-integrationwebsphere-7

connection.getConnetion() returns 404 code on WebSphere7


I have implemented a REST service using Spring Integration. When I try to access the service manually using main function, It is working fine. I also tested the service using REST Client in Google Chrome and that worked. But the service is coming back with responseCode 404 on WebSphere server. So I am facing the issue when I deploy the code on higher environment.

  URL u = new URL("http://localhost:8080/MyApplication/testRestService");
  URLConnection uc = u.openConnection();
  HttpURLConnection connection = (HttpURLConnection) uc;

  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Accept","*/*");      
  connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

  OutputStream out = connection.getOutputStream();
  Writer wout = new OutputStreamWriter(out);

  //helper function that gets a string from a dom Document
  String input = jsonInput;
  wout.write(input.getBytes());        
  wout.flush();
  wout.close();

  // Response
  int responseCode = connection.getResponseCode();

Is is dependent on server, so its coming back with response code 404 ? Do we need any server side configuration ?

Any suggestion will be appreciated.


Solution

  • I tried with Apache HTTP Client and the code is working on WebSphere now. Still I am not able to find the reason why java.net.HttpURLConnection was not working on WebSphere.

    Please find my updated code below :

    DefaultHttpClient httpClient = null;
            HttpPost postRequest = null;
            StringEntity inputEntity = null;
            HttpResponse response = null;
            try{
    
    
                //RETREIVE WEB SERVICE URL FROM DB
                String callbackURL = "http://localhost:8080/MyApplication/testRestService";
                httpClient = new DefaultHttpClient();
                postRequest = new HttpPost(callbackURL);            
                String inputData = request.toString();
    
                inputEntity = new StringEntity(inputData);
                inputEntity.setContentType("application/x-www-form-urlencoded");
                postRequest.setEntity(inputEntity);
    
                response = httpClient.execute(postRequest);
    
    
    
                if (response.getStatusLine().getStatusCode() != 201 && response.getStatusLine().getStatusCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : "+ response.getStatusLine().getStatusCode());
                }
    
                //System.out.println("HTTP Response Code :"+response.getStatusLine().getStatusCode());
                LOGGER.debug("HTTP Response Code :"+response.getStatusLine().getStatusCode());
    
                httpClient.getConnectionManager().shutdown();
            }catch(IOException ex){
                ex.printStackTrace();
                throw ex;
            }finally{
                httpClient.getConnectionManager().shutdown();
                httpClient = null;
                postRequest = null;
                inputEntity = null;
                response = null;
            }