我在发布到我的java HTTPServlet时遇到问题。我从我的tomcat服务器上得到"HTTP状态405-HTTP方法GET不受此URL支持"。当我调试servlet时,登录方法从未被调用。我认为这是tomcat中的URL映射问题…
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();
}
}
我从本教程开始学习:http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
您在概念上犯了一个重大错误。您正在将JAX-RS API与Servlet API混合使用。使用@Path
,您基本上就是在注册JAX-RS Web服务。该类恰好是从HttpServlet
扩展而来的,这并不能使它自动成为一个完整的servlet。然后,您将其映射为web.xml
中的独立servlet(这样的实例将忽略所有与JAX-RS相关的注释,如@Path
等等!),但您没有按照servlet API指南覆盖servletneneneba API的doGet()
方法。这解释了HTTP405错误,即调用servlet的URL时找不到GET
方法。
我不确定您在尝试什么,但如果您想要JAX-RS Web服务,而不是servlet,那么您应该首先完成教程的这一部分。它描述了如何在web.xml
中配置和注册JAX-RS Web服务。然后,从JAX-RSWebService类中完全去掉extends HttpServlet
,并从web.xml
中删除错误的映射。
还请阅读教程的指导文本,而不是盲目地复制粘贴代码片段,并在不了解自己在做什么的情况下到处更改类/包名称。