HTTP error code 405: tomcat Url mapping issue
- by Andrew
I am having trouble POSTing to my java HTTPServlet. I am getting "HTTP Status 405 - HTTP method GET is not supported by this URL" from my tomcat server". When I debug the servlet the login method is never called. I think it's a url mapping issue within tomcat...
web.xml
<servlet-mapping>
<servlet-name>faxcom</servlet-name>
<url-pattern>/faxcom/*</url-pattern>
</servlet-mapping>
FaxcomService.java
@Path("/rest")
public class FaxcomService extends HttpServlet{
private FAXCOM_x0020_ServiceLocator service;
private FAXCOM_x0020_ServiceSoap port;
@GET
@Produces("application/json")
public String testGet()
{
return "{ \"got here\":true }";
}
@POST
@Path("/login")
@Consumes("application/json")
// @Produces("application/json")
public Response login(LoginBean login)
{
ArrayList<ResultMessageBean> rm = new ArrayList<ResultMessageBean>(10);
try {
service = new FAXCOM_x0020_ServiceLocator();
service.setFAXCOM_x0020_ServiceSoapEndpointAddress("http://cd-faxserver/faxcom_ws/faxcomservice.asmx");
service.setMaintainSession(true); // enable sessions support
port = service.getFAXCOM_x0020_ServiceSoap();
rm.add(new ResultMessageBean(port.logOn(
"\\\\CD-Faxserver\\FaxcomQ_API",
/* path to the queue */
login.getUserName(),
/* username */
login.getPassword(),
/* password */
login.getUserType()
/* 2 = user conf user */
)));
} catch (RemoteException e) {
e.printStackTrace();
}
catch (ServiceException e) {
e.printStackTrace();
}
// return rm;
return Response.status(201).entity(rm).build();
}
@POST
@Path("/newFaxMessage")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<ResultMessageBean> newFaxMessage(FaxBean fax) {
ArrayList<ResultMessageBean> rm = new ArrayList<ResultMessageBean>();
try {
rm.add(new ResultMessageBean(port.newFaxMessage(
fax.getPriority(),
/* priority: 0 - low, 1 - normal, 2 - high, 3 - urgent */
fax.getSendTime(),
/* send time */
/* "0.0" - immediate */
/* "1.0" - offpeak */
/* "9/14/2007 5:12:11 PM" - to set specific time */
fax.getResolution(),
/* resolution: 0 - low res, 1 - high res */
fax.getSubject(),
/* subject */
fax.getCoverpage(),
/* cover page: "" – default, “(none)� – no cover page */
fax.getMemo(),
/* memo */
fax.getSenderName(),
/* sender's name */
fax.getSenderFaxNumber(),
/* sender's fax */
fax.getRecipients().get(0).getName(),
/* recipient's name */
fax.getRecipients().get(0).getCompany(),
/* recipient's company */
fax.getRecipients().get(0).getFaxNumber(),
/* destination fax number */
fax.getRecipients().get(0).getVoiceNumber(),
/* recipient's phone number */
fax.getRecipients().get(0).getAccountNumber()
/* recipient's account number */
)));
if (fax.getRecipients().size() > 1) {
for (int i = 1; i < fax.getRecipients().size(); i++)
rm.addAll(addRecipient(fax.getRecipients().get(i)));
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rm;
}
}
Main.java
private static void main(String[] args)
{
try {
URL url = new URL("https://andrew-vm/faxcom/rest/login");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
FileInputStream jsonDemo = new FileInputStream("login.txt");
OutputStream os = (OutputStream) conn.getOutputStream();
os.write(IOUtils.toByteArray(jsonDemo));
os.flush();
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
// Don't want to disconnect - servletInstance will be destroyed
// conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am working from this tutorial: http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/