我使用球衣JAX-Rs作为Web服务来查询mysql。我尝试通过混合移动应用程序使用Web服务。
我指的是这个 http://coenraets.org/blog/2011/11/building-restful-services-with-java- using-jax-rs-and-jersey-sample- application/#comment-440541
在服务器端,以下代码在tomcat server7中运行以查询sql
@Path("/employees") public class EmployeeResource { EmployeeDAO dao = new EmployeeDAO(); @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public List<Employee> findAll() { return dao.findAll(); } }
公共类EmployeeDAO {
public List<Employee> findAll() { List<Employee> list = new ArrayList<Employee>(); Connection c = null; String sql = "SELECT e.id, e.firstName, e.lastName, e.title FROM employee as e"; try { c = ConnectionHelper.getConnection(); Statement s = c.createStatement(); ResultSet rs = s.executeQuery(sql); while (rs.next()) { list.add(processSummaryRow(rs)); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { ConnectionHelper.close(c); } return list; } protected Employee processSummaryRow(ResultSet rs) throws SQLException { Employee employee = new Employee(); employee.setId(rs.getInt("id")); employee.setFirstName(rs.getString("firstName")); employee.setLastName(rs.getString("lastName")); employee.setTitle(rs.getString("title")); /*employee.setPicture(rs.getString("picture")); employee.setReportCount(rs.getInt("reportCount"));*/ return employee; }
}
我已经创建了数据库目录,表名称为employee,具有字段id,firstName,lastName,title。
现在,我在 同一项目 的web-content文件夹中拥有html文件。
<!DOCTYPE HTML> <html> <head> <title>Employee Directory</title> </head> <body> <h1>Employee Directory</h1> <ul id="employeeList"></ul> <script src="http://code.jquery.com/jquery-1.7.min.js"></script> <script src="js/employeelist.js"></script> </body> </html>
员工列表脚本
getEmployeeList(); function getEmployeeList() { $.getJSON(serviceURL + 'employees', function(data) { $('#employeeList li').remove(); var employees = data.employee; $.each(employees, function(index, employee) { $('#employeeList').append( '<li><a href="employeedetails.html#' + employee.id + '">' + employee.firstName + ' ' + employee.lastName + ' (' + employee.title + ')</a></li>'); }); }); }
这将在“索引”页面中显示确切的员工详细信息。
现在我已经在 另一个项目 中创建了一个html页面,我将在其中放置与上面指定的相同的$ .getJSON调用,这将在控制台中引发错误,
XMLHttpRequest cannot load http://localhost:8181/jQueryJAXRS/rest/employees. Origin null is not allowed by Access-Control-Allow-Origin.
实际上我尝试开发具有客户端和服务器基础结构的应用程序,因此我需要在客户端中包含html文件才能在服务器端使用jersey Web服务。如果我从$.getJSON或$ .ajax调用非常有用另一个项目中的html文件以使用Web服务。
when i use this url http://localhost:8181/jQueryJAXRS/rest/employees in my browser
它显示xml
<employees> <employee> <firstName>john</firstName> <id>1</id> <lastName>doe</lastName> <reportCount>0</reportCount> <title>trainee</title> </employee> <employee> <firstName>james</firstName> <id>2</id> <lastName>dane</lastName> <reportCount>0</reportCount> <title>developer</title> </employee> <employee> <firstName>ramsy</firstName> <id>4</id> <lastName>stuart</lastName> <reportCount>0</reportCount> <title>QA</title> </employee> </employees>
但是当我尝试通过脚本方式时,它将显示发生CORS错误。提出一些想法对我有帮助。
提前致谢 。
您必须使用CORS。
为了那个原因:
<filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowGenericHttpRequests</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.allowSubdomains</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, HEAD, POST, OPTIONS</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Content-Type, X-Requested-With</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>X-Test-1, X-Test-2</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>cors.maxAge</param-name> <param-value>-1</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/EmployeeResource</url-pattern> </filter-mapping>