HTTP错误代码405:tomcat Url映射问题

时间:2013-06-25 16:18:15

标签: servlets jax-rs

我在发布到我的java HTTPServlet时遇到问题。我从tomcat服务器获取“HTTP状态405 - 此URL不支持HTTP方法GET”。当我调试servlet时,从不调用login方法。我认为这是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/

1 个答案:

答案 0 :(得分:3)

你犯了一个重大的概念错误。您将JAX-RS APIServlet API混合在一起。使用@Path,您基本上是在注册JAX-RS Web服务。该类恰好从HttpServlet扩展,并不能使它自动成为一个完整的servlet。然后,您将其映射为web.xml中的独立servlet(这样的实例将忽略所有与JAX-RS相关的注释,如@Path等等),但您没有' t根据Servlet API指南覆盖servlet API的doGet()方法。这解释了HTTP 405错误,即在调用servlet的URL时无法找到GET方法。

我不确定你在尝试什么,但如果你想要一个JAX-RS网络服务,而不是一个servlet,那么你应该首先完成你找到的教程的this section。它描述了如何在web.xml中配置和注册JAX-RS Web服务。然后,完全删除JAX-RS Web服务类中的extends HttpServlet,并从web.xml中删除错误的映射。

请同时阅读教程的指导文本,而不是盲目地复制代码片段并更改例如这里和那里的课程/包名称并没有真正理解你在做什么。