根据JAX-RS规范,我无法为用Java编写的RESTful Web服务后端生成基于Javascript的CRUD UI前端
我使用Netbeans IDE在Java中创建了以下RESTful web服务:根据JAX-RS规范,我无法为用Java编写的RESTful Web服务后端生成基于Javascript的CRUD UI前端,rest,backbone.js,java-ee-7,Rest,Backbone.js,Java Ee 7,我使用Netbeans IDE在Java中创建了以下RESTful web服务: package entities.service; import entities.Product; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.ws.rs.Co
package entities.service;
import entities.Product;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Stateless
@Path("entities.product")
public class ProductFacadeREST extends AbstractFacade<Product> {
@PersistenceContext(unitName = "DemoBackendPU")
private EntityManager em;
public ProductFacadeREST() {
super(Product.class);
}
@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void create(Product entity) {
super.create(entity);
}
@PUT
@Path("{id}")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void edit(@PathParam("id") Integer id, Product entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Product find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Product> findAll() {
return super.findAll();
}
@GET
@Path("{from}/{to}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Product> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String countREST() {
return String.valueOf(super.count());
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
导航到web服务URL时,我能够看到产品实体的XML表示,如下所示
<products>
<product>
<description>Good</description>
<id>1</id>
<name>Microsoft 01</name>
<price>5500</price>
<quantity>10</quantity>
</product>
<product>
<description>Good</description>
<id>2</id>
<name>Microsoft 02</name>
<price>6500</price>
<quantity>20</quantity>
</product>
....
....
....
</products>
好
1.
微软01
5500
10
好
2.
微软02
6500
20
....
....
....
但是,在运行位于不同项目中的HTML页面并使用上述RESTful web服务时,我遇到了以下错误,并且网格中没有填充数据
加载资源失败:服务器响应状态为500(内部服务器错误)(16:33:56:252 |错误,网络)
在
XMLHttpRequest无法加载“上面的web服务URL”
请求的资源上不存在“Access Control Allow Origin”标头
因此,不允许访问源“”
响应的HTTP状态代码为500。(16:33:56:254 |错误,javascript)
在public_html/Products.html
我正在使用Backbone.js控制器
可能需要注意的是,我的客户端HTML5网页运行在不同于RESTfulWeb服务域的域中
为什么会出现内部服务器错误,为什么XMLHttpRequest无法加载REST资源?为什么它会说“请求的资源上不存在‘访问控制允许源站’标头”。我的跨源站资源共享筛选器是否缺少某些内容?请帮助。您还应该发布异常本身。一个500错误可能是很多东西…让cors工作有点麻烦。将您所拥有的内容与其他指定cors标头的示例进行比较,如果我查看此示例,您的allowed headers字段缺少“options”标头:。我知道您没有使用Spring,但cors不是特定于技术的。(搜索“CORS的筛选请求”)
<products>
<product>
<description>Good</description>
<id>1</id>
<name>Microsoft 01</name>
<price>5500</price>
<quantity>10</quantity>
</product>
<product>
<description>Good</description>
<id>2</id>
<name>Microsoft 02</name>
<price>6500</price>
<quantity>20</quantity>
</product>
....
....
....
</products>