【问题标题】:Getting 404 Not Found - while accessing valid REST service with Jersey + ExtJS找不到 404 - 使用 Jersey + ExtJS 访问有效的 REST 服务时
【发布时间】:2014-10-06 19:00:13
【问题描述】:

我正在使用 ExtJS,需要访问 REST 服务。我尝试使用 Spring 的 MVC REST 支持功能来做到这一点,并且效果很好。

现在我必须选择 Jersey(JAX-RS)。尝试使用 Jersey 时,我不断收到 404 错误。我认为注释、URL 映射等都很好(因为类似的也适用于 Spring)

下面是相关代码sn-ps。

Web.xml

   <servlet>
    <servlet-name>spring-jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.myProducts.controller</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

<servlet-mapping>
    <servlet-name>spring-jersey</servlet-name>
    <url-pattern>/jersey/*</url-pattern>
</servlet-mapping>

App.js

Ext.onReady(function() {

    var store = Ext.create('Ext.data.Store', {
        autoLoad: true,
        autoSync: true,
        model: 'Product',
        proxy: {
            type: 'rest',
            url: 'products',
            format: 'json',
            headers: {
                'Content-Type': 'application/json'
            },
            reader: {
                type: 'json',
                root: 'data'
            },
            writer: {
                type: 'json'
            },
            api: {
                create: 'products/createJ/',
                read: 'products/readJ/',
                update: 'products/editJ/',
                destroy: 'products/deleteJ/'
            }
        }
    });

控制器:

@Component
@Path("products/jersey/products")
public class JerseyProductsController {


    @Autowired
    private ProductService productService;

    @POST
    @Path("createJ/{id}")
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    public Product createJ(@PathParam("id") int id, @RequestBody Product myProduct) {
        myProduct.setId(id);
        return productService.create(myProduct);
    }

    @PUT
    @Path("editJ/{id}")
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    @Produces(MediaType.APPLICATION_JSON_VALUE) 
    public Product editJ(@PathParam("id") int id, @RequestBody Product myProduct) {
        myProduct.setId(id);
        return productService.update(myProduct);
    }


    @DELETE
    @Path("deleteJ/{id}")
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    @Produces(MediaType.APPLICATION_JSON_VALUE) 
    public Product deleteJ(@PathParam("id") int id) {
        return productService.delete(id);
    }

    @GET
    @Path("readJ/")
    @Consumes(MediaType.APPLICATION_JSON_VALUE)
    @Produces(MediaType.APPLICATION_JSON_VALUE)
    public List<Product> allProductsJ() {
        return productService.getAll();
    }
}

我得到以下 URL 的 404:

Request URL:http://localhost:4080/MyProducts/products/jersey/products/readJ/.json?_dc=1407930853131&page=1&start=0&limit=25
Request Method:GET
Status Code:404 Not Found

请让我知道遗漏了什么。

【问题讨论】:

    标签: rest spring-mvc extjs


    【解决方案1】:

    您可能需要在您的网址中使用/jersey/products/jersey,因为您使用的是&lt;url-pattern&gt;/jersey/*&lt;/url-pattern&gt;

    【讨论】:

      【解决方案2】:

      不确定是什么问题,但您可以尝试以下一种:

      <url-pattern>*.do</url-pattern>
      

      我相信它是因为它可以捕获以 .do 结尾的请求,并且减少了在 URL 之间存在某种模式的麻烦。

      【讨论】:

      • 使用 *.do URL 模式!
      【解决方案3】:

      我认为您的 servlet 映射不正确。您的 Url-Pattern 是 /jersey/* 但在您的 url 中是 /MyProducts/*

      把它放在你的 web.xml 中

      <servlet-mapping>
      <servlet-name>spring-jersey</servlet-name>
      <url-pattern>/MyProducts/*</url-pattern>
      

      【讨论】:

      • MyProducts 是我的应用名称和 /jersey/* - 满足我尝试访问的 URL 的条件。下面的一个对 Spring 很有效: spring-restorg.springframework.web.servlet.DispatcherServletspring-rest/spring/* 所以我认为这不是问题。顺便说一句,我尝试了许多排列(包括您建议的排列)但没有运气:(
      • 看看你的url,/MyProducts/是你的应用程序,products/jersey/products/是你的类,/readJ/是你要调用的方法,你需要的是你的/jersey /* 也许它适用于这个 url //localhost:4080/MyProducts/jersey/products/jersey/products/readJ/.json?_dc=1407930853131&page=1&start=0&limit=25
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2012-05-01
      • 2022-01-16
      • 2016-03-17
      • 2011-09-28
      相关资源
      最近更新 更多