0

我在发布到我的 java HTTPServlet 时遇到问题。我从我的 tomcat 服务器收到“HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET”。当我调试 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; 
    }

}

主.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/

4

1 回答 1

3

你犯了一个重大的概念错误。您正在将JAX-RS APIServlet API混合使用。@Path你基本上是在注册一个 JAX-RS 网络服务。该类恰好扩展自HttpServlet并不会自动使其成为一个完全值得的 servlet。然后,您将其映射为一个独立的 servlet web.xml(这样的实例将忽略所有与 JAX-RS 相关的注释@Path,等等!),但您没有doGet()按照 Servlet API 指南覆盖 servlet API 的方法。GET这解释了调用 servlet 的 URL 时找不到该方法的 HTTP 405 错误。

我不确定你在尝试什么,但如果你想要一个 JAX-RS web 服务,而不是一个 servlet,那么你应该首先完成你找到的教程的这一部分。它描述了如何在web.xml. 然后,extends HttpServlet从您的 JAX-RS webservice 类中完全删除 ,并从web.xml.

还请阅读教程的指导文本,而不是仅仅盲目地复制粘贴代码片段和更改例如类/包名称而不真正理解您在做什么。

于 2013-06-25T16:46:11.907 回答