我正在尝试在Netbeans 8.2(server-tomcat 8.0.27.0(上的Web应用程序中创建一个Web服务,该服务可以连接到postgres上的数据库并读取名为"test"的表。 我正在下载此代码.java (名为 serve 的包中的 Web 服务(
下载.java
@WebService(serviceName = "download")
public class Download {
Connection con=null;
private DataSource getJdbcPostgres() throws NamingException, SQLException {
Context c = new InitialContext();
DataSource ds=(DataSource) c.lookup("java:comp/env/jdbc/postgres");
con=ds.getConnection();
return ds;
}
@WebMethod(operationName = "download")
public String download(@WebParam(name = "username")String username, @WebParam(name = "id")String id) throws ClassNotFoundException, SQLException {
String sql = "select * from test where id="+id;
Class.forName("org.postgresql.Driver");
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
rs.next();
return "";
}
}
这是我的上下文.xml文件(在元信息中(
上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/service2">
<Resource name="jdbc/postgres" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/postgres"
username="someusername" password="somepassword" maxTotal="20" maxIdle="10"
maxWaitMillis="-1"/>
</Context>
这是 web.xml(在 WEB-INF 中(:
网络.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
清理并构建命令成功,但它不会部署在 Apache Tomcat 服务器上 错误消息(我尝试部署它时输出(:
Checking data source definitions for missing JDBC drivers...
Undeploying ...
undeploy?path=/service2
OK - Undeployed application at context path /service2
In-place deployment at D:NetBeansProjectsservice2buildweb
Deployment is in progress...
deploy?config=file%3A%2FC%3A%2FUsers%2FTRAINE%7E3%2FAppData%2FLocal%2FTemp%2Fcontext1366288511044657094.xml&path=/service2
FAIL - Deployed application at context path /service2 but context failed to start
D:NetBeansProjectsservice2nbprojectbuild-impl.xml:1094: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 2 seconds)
当我尝试将服务器更改为GlassFish 4.1.1时,它说:
Severe: WSSERVLET11: failed to parse runtime descriptor:
com.sun.xml.ws.spi.db.DatabindingException:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
javax.xml.transform.SourceLocator is an interface, and JAXB can't handle
interfaces.
this problem is related to the following location:
at javax.xml.transform.SourceLocator
at public javax.xml.transform.SourceLocator
serve.jaxws.TransformerConfigurationExceptionBean.locator
at serve.jaxws.TransformerConfigurationExceptionBean
Caused by: com.sun.xml.ws.spi.db.DatabindingException:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
javax.xml.transform.SourceLocator is an interface, and JAXB can't handle
interfaces.
this problem is related to the following location:
at javax.xml.transform.SourceLocator
at public javax.xml.transform.SourceLocator
serve.jaxws.TransformerConfigurationExceptionBean.locator
at serve.jaxws.TransformerConfigurationExceptionBean
Severe: Exception while loading the app
Severe: Undeployment failed for context /service2
Severe: Exception while loading the app : java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.apache.catalina.LifecycleException: javax.servlet.ServletException:
com.sun.xml.ws.transport.http.servlet.WSServletException: WSSERVLET11: failed to
parse runtime descriptor: com.sun.xml.ws.spi.db.DatabindingException:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
javax.xml.transform.SourceLocator is an interface, and JAXB can't handle
interfaces.
this problem is related to the following location:
at javax.xml.transform.SourceLocator
at public javax.xml.transform.SourceLocator serve.jaxws.TransformerConfigurationExceptionBean.locator
at serve.jaxws.TransformerConfigurationExceptionBean
我是这个领域的新手,所以如果我在某处出错或遗漏了什么,请帮助我!
我不知道为什么会发生这种情况,但我只是放了 try-catch 而不是投掷,它正在部署!
@WebMethod(operationName = "download")
public String download(@WebParam(name = "username")String username, @WebParam(name = "id")String id) {
try {
String sql="select * from test";
Connection conn=myDatasource.getConnection();
PreparedStatement pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
}
catch (SQLException ex) {
Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
}
return "someString";
}
谁能建议为什么它不早点工作?虽然我的问题已经解决了,但我很好奇"抛出异常"出了什么问题! 附言这次我在 GlassFish 4.1.1 上运行它