我正在使用一个充当宁静的web服务的类。当我调用一个返回html输出的方法时,我得到了java.lang.IllegalStateException

时间:2015-05-25 05:19:33

标签: java web-services rest

下面是我的restful web服务类,其中包含一个生成 HTML 输出的方法。我正在使用Apache Tomcat V8.0.20

@Path("/cardvalidate")
public class cardclass 
{

     @GET
     @Produces(MediaType.TEXT_HTML)
     public String validate(@QueryParam("name") String name,@QueryParam("cardno") String cardno,@QueryParam("cvv") String cvv,@QueryParam("amount") String amount)
     {


        String returnparam = new String();
        Connection con = null;

        try
        {


            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost/f1","ramji","mysql");

            PreparedStatement ps = con.prepareStatement("Select 1 from credit_card where name = ? and cardnumber = ? and cvv = ?"); 

            ps.setString(1, name);
            ps.setString(2, cardno);
            ps.setInt(3, Integer.parseInt(cvv));

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                    if(Integer.parseInt(amount)>100000){
                        returnparam= "<html><body><h3>Transaction amount has exceeded the limit. Transaciton cancelled</h3></body></html>";
                    } else {
                        returnparam = "<html><body><h3>Transaciton successfull</h3></body></html>";
                    }

                } else {
                    returnparam = "<html><body><h3>Invalid card details. Transaction cancelled</h3></body></html>";
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    con.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

        return returnparam;

    }
}

当我通过网址http://localhost:8080/final_project/rest/cardvalidate?name=ramji&cardno=12345&cvv=663&amount=1000

调用此方法时

'final_project'是应用程序名称,'rest'是restful服务的基本网址,'cardvalidate'用于调用 cardclass 及其方法。 我收到了以下错误:

 org.glassfish.jersey.server.ServerRuntime$Responder writeResponse
 SEVERE: Error while closing the output stream in order to commit response.
 java.lang.IllegalStateException: getWriter() has already been called for this response
 at  org.apache.catalina.connector.Response.getOutputStream(Response.java:544)
at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:195)
at javax.servlet.ServletResponseWrapper.getOutputStream(ServletResponseWrapper.java:100)
at org.glassfish.jersey.servlet.internal.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:176)
at org.glassfish.jersey.server.ServerRuntime$Responder$1.getOutputStream(ServerRuntime.java:654)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:200)
at org.glassfish.jersey.message.internal.CommittingOutputStream.flushBuffer(CommittingOutputStream.java:305)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:261)
at org.glassfish.jersey.message.internal.CommittingOutputStream.close(CommittingOutputStream.java:276)
at org.glassfish.jersey.message.internal.OutboundMessageContext.close(OutboundMessageContext.java:839)
at org.glassfish.jersey.server.ContainerResponse.close(ContainerResponse.java:412)
at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:737)
at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:421)
at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:411)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:401)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:523)
at final_project.myfilter.doFilter(myfilter.java:45)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

试试这个,这应该可以解决你的问题。

@Path("/cardvalidate")
public class cardclass 
{

 @GET
 @Produces(MediaType.TEXT_HTML)
 public String validate(@QueryParam("name") String name,@QueryParam("cardno") String cardno,@QueryParam("cvv") String cvv,@QueryParam("amount") String amount)
 {


        String returnparam = new String();
        Connection con = null;

        try
        {


            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection("jdbc:mysql://localhost/f1","ramji","mysql");

            PreparedStatement ps = con.prepareStatement("Select * from credit_card where name = ? and cardnumber = ? and cvv = ?"); 

            ps.setString(1, name);
            ps.setString(2, cardno);
            ps.setInt(3, Integer.parseInt(cvv));

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                    if(Integer.parseInt(amount)>100000){
                        returnparam= "<html><body><h3>Transaction amount has exceeded the limit. Transaciton cancelled</h3></body></html>";
                    } else {
                        returnparam = "<html><body><h3>Transaciton successfull</h3></body></html>";
                    }

                } else {
                    returnparam = "<html><body><h3>Invalid card details. Transaction cancelled</h3></body></html>";
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try {
                    con.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

        return returnparam;

    }
}

您的SQL-Query将始终返回1作为输出。当您访问列时,它将无法找到表的那些列。因此,您必须使用SELECT *或指定所需的列名称。

您的rs变量包含一个ROW,它有一个记录,列名为1,因为您将结果提取为 SELECT 1 FROM credit_card .... 所以它将返回记录,如ColumnName = 1RecordValue = 1

因此,请尝试更改您的查询,然后重新运行您的代码。