Reputation: 21
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/
Upvotes: 0
Views: 5260
Reputation: 1109665
You're making a major conceptual mistake. You're mixing JAX-RS API with Servlet API. With @Path
you're basically registering a JAX-RS webservice. That the class happen to extend from HttpServlet
doesn't make it automagically a fullworthy servlet. Then, you mapped it as an independent servlet in web.xml
(such an instance would ignore all JAX-RS related annotations like @Path
and so on!), but you didn't override the servlet API's doGet()
method as per Servlet API guidelines. That explains the HTTP 405 error that the GET
method couldn't be found when invoking the servlet's URL.
I'm not sure what you're trying, but provided that you want a JAX-RS webservice, not a servlet, then you should first be working through this section of the tutorial you found. It describes how to configure and register the JAX-RS webservice in web.xml
. Then, get rid of the extends HttpServlet
from your JAX-RS webservice class altogether and remove the wrong mapping from web.xml
.
Please also read the tutorial's guiding text instead of only blindly copypasting code snippets and changing e.g. class/package names here and there without actually understanding what you're doing.
Upvotes: 3