我创建了一个 Web 应用程序,可以通过 maven eclipse 部署在 Heroku 上。
组 ID: org.glassfish.jersey.archetypes
工件 ID: jersey-heroku-webapp
版本:2.17
然后我按照这个指南(1.5)将其推送到 Heroku。
https://jersey.java.net/documentation/latest/getting-started.html#heroku-webapp
我无法从 http 链接访问我的 apple
类,如下所示:
https://salty-refuge-2027.herokuapp.com/apple
我收到错误500
我之前已经在没有 JDBC 过程的情况下测试过它,并且得到了输出 Hello, from apple class
因此我想这取决于我对 Heroku Postgres
的实现
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-java
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("apple")
public class Apple {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
try {
getConnection();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Hello, from apple class";
}
private static Connection getConnection() throws URISyntaxException, SQLException {
//I have modified my orginal ingredients.
URI dbUri = new URI(System.getenv("postgres://mglfe545z6ixsgk:yf8gyK1hBh3jknzqepLnajWRLv@
ec2-60-16-433-222.compute-1.amazonaws.com:5432/d5kal1r7jtavr9"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':'
+ dbUri.getPort() + dbUri.getPath();
Connection con = DriverManager.getConnection(dbUrl, username, password);
System.out.println("It works");
return con;
}
}
感谢任何帮助。
最佳答案
您应该调用System.getenv("DATABASE_URL")
。您正在使用环境变量的值调用 getenv
,这将返回 null。
关于java - Heroku 上的错误 501,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30210217/