Getting 404 Not Found - while accessing valid REST

2019-06-05 04:49发布

I am working on ExtJS and need to access the REST services. I tried doing that with Spring's MVC REST supporting functionality and it worked well.

Now its kind of mandatory that I have to go with Jersey(JAX-RS). When tried with Jersey, I keep getting 404 error. I think the annotations, URL mapping, etc are fine (since the similar ones worked for Spring)

Below are the relevant code snippets.

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/'
            }
        }
    });

Controller :

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

I get 404 for below URL:

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

Kindly let me know what is being missed.

3条回答
够拽才男人
2楼-- · 2019-06-05 05:00

Not sure what's the issue but you could try below one :

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

I believe its because it could capture requests ending with .do and it reduced the headache of having some pattern in between the URL.

查看更多
一纸荒年 Trace。
3楼-- · 2019-06-05 05:04

I think your servlet mapping is incorrect. Your Url-Pattern is /jersey/* but in your url is /MyProducts/*

put this in your web.xml

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

查看更多
乱世女痞
4楼-- · 2019-06-05 05:13

You may need to use /jersey/products/jersey in your URL since you are having <url-pattern>/jersey/*</url-pattern>

查看更多
登录 后发表回答