当前位置: 首页 > 面试题库 >

找不到404-使用Jersey + ExtJS访问有效的REST服务时

何安宜
2023-03-14
问题内容

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

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

以下是相关的代码段。

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();
    }
}

我收到以下网址的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

请让我知道错过了什么。


问题答案:

不知道是什么问题,但是您可以尝试以下一种方法:

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

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



 类似资料:
  • 我试图创建spring rest服务,它是由我自己的oauth2资源服务器自动引诱的。我创建了资源服务器: {“error”:“server_error”,“error_description”:“java.io.NotSerializableException:org.springframework.security.crypto.bcrypt.bcryptPasswordenCoder”} 在

  • 我正在尝试使用Payara服务器为Eclipse中的预订系统开发Jersey服务器应用程序。当我运行这个项目时,我会得到“HTTP状态404-未找到”。我查看了一些教程和StackOverflow帖子,但没有找到错误。有人能帮我吗? BookingService界面: BookingService实现: 波姆。xml: 编辑:当我研究这个问题时,我在网上读到。使用jersey 2时,xml文件不必

  • 如果添加PowerMockIgnore({“javax.ws.”,“org.glassfish.”}),请参见下面的异常:

  • 问题内容: 谁能提供给我一个代码示例,以使用Spring Rest模板访问通过https保护的REST服务URL。 我有证书,用户名和密码。服务器端使用了基本身份验证,我想创建一个可以使用提供的证书,用户名和密码(如果需要)连接到该服务器的客户端。 问题答案:

  • 嗨,我有一个restendpointxyz。com/test/create,其中预期的内容类型为application/json,内容为 在具有数组的body中还有一些其他字段。 我在Spring rest控制器中使用rest模板来访问上述endpoint,我还想传递数据。我不确定endpoint端使用什么域模型将json中的数据从客户端映射到服务器端。 如何使用rest模板使用上述数据命中上述e

  • 我想构建一个restful服务/API。我使用了一些像play这样的框架来构建它,但我想尝试其他更有效的方法。我听说Jersey是构建rest API的常用库,Spring也是一个很好的框架。但我也看到了一些类似Spring+Jersey的解决方案。因此,我对那些rest API解决方案有点困惑。 我的目标是构建几个将JSON作为输入/输出的rest API。我有jar文件作为后端处理逻辑来处理输

  • 我无法获得足够的权限来使用我的应用程序访问Azure Key Vault(而不是通过用户登录)。以下是我的设置: 我已经给了我的名为“keyvault”的应用程序所有的许可。 我的应用程序注册了Azure Active Directory。我已经允许它进入密钥库:

  • 起初,在我的web服务器中,我只有一个REST servlet。类似于: 和web.xml: 但是后来我想向服务器添加一些静态html,所以我将servlet映射更新为/rest/* 控制器servlet类的@path指令从“/”到“/rest”。一切都很好,但是controller的子资源或方法与@path指令停止工作…IE: null 我使用了trace util,得到了以下结果: 对于/[a