PDA

View Full Version : Getting 404 Not Found - while accessing valid REST service with Jersey + ExtJS



snehalmasne
13 Aug 2014, 4:51 AM
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-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>spring-rest</servlet-name>
<url-pattern>/spring/*</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.

Gary Schlosberg
13 Aug 2014, 5:40 AM
I've never worked with Jersey before. Do you see the data you are expecting when you go to your URL separately in your browser?
http://localhost:4080/MyProducts/products/jersey/products/readJ/.json

Does the server log the request?

snehalmasne
13 Aug 2014, 6:05 AM
No Gary, actually that's the issue.

I get 404 with description as 'The requested resource is not available.' when hitting seperately from browser.

Firstly I thought its about wrong mapping, but it worked well with Spring REST framework. It blows when tried with Jersey.

Scott Mildenberger
14 Aug 2014, 7:16 AM
What URL is going to the server when you use Spring?