connection.getConnetion() returns WebSphere7 上的 404 代码
connection.getConnetion() returns 404 code on WebSphere7
我已经使用 Spring 集成实现了 REST 服务。
当我尝试使用 main 函数手动访问该服务时,它工作正常。
我还在 Google Chrome 中使用 REST Client 测试了该服务并且有效。但是该服务在 WebSphere 服务器上返回响应代码 404。所以我在更高的环境中部署代码时遇到了这个问题。
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();
是否依赖于服务器,所以它返回响应代码 404?我们需要任何服务器端配置吗?
如有任何建议,我们将不胜感激。
我尝试使用 Apache HTTP 客户端,代码现在可以在 WebSphere 上运行。我仍然无法找到 java.net.HttpURLConnection 无法在 WebSphere 上工作的原因。
请在下面找到我更新的代码:
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;
}
为什么 URLConnection
和 httpClient
使用不同的 ContentType
?
请显示您的 REST 服务配置:404
表示 Not found
。因此,您在请求中使用(或不使用)某些选项,使其与服务器的 RequestMapping
不匹配。
我已经使用 Spring 集成实现了 REST 服务。 当我尝试使用 main 函数手动访问该服务时,它工作正常。 我还在 Google Chrome 中使用 REST Client 测试了该服务并且有效。但是该服务在 WebSphere 服务器上返回响应代码 404。所以我在更高的环境中部署代码时遇到了这个问题。
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();
是否依赖于服务器,所以它返回响应代码 404?我们需要任何服务器端配置吗?
如有任何建议,我们将不胜感激。
我尝试使用 Apache HTTP 客户端,代码现在可以在 WebSphere 上运行。我仍然无法找到 java.net.HttpURLConnection 无法在 WebSphere 上工作的原因。
请在下面找到我更新的代码:
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;
}
为什么 URLConnection
和 httpClient
使用不同的 ContentType
?
请显示您的 REST 服务配置:404
表示 Not found
。因此,您在请求中使用(或不使用)某些选项,使其与服务器的 RequestMapping
不匹配。