1

I have written my ajax post in my Java Script file like this: Where obj1 is a Stringified JSON object.

$.ajax({
    url:'./rest/input/post',
    type: 'POST',
    data:obj1,
    contentType: 'application/json',
    dataType:'json',
    success:function(data)
    {
        var values = JSON.stringify(data);
        alert(values);
    },
},'json');

I am passing it to a Java class which is as below:

@Path("/input")
public class InputResponse {
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON) 
@Produces(MediaType.APPLICATION_JSON)
     public Input postInputRecord(Input obj){
         return obj;
     }
}

My Web.xml file is as below:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>CAD</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.cad.example.response</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.cad.data.model</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

This is the maven project: I am running it on a Jetty Server, while invoking a url "http://localhost:8001/project_name/rest/input/post" But it is giving me 404 error. There is no error in console except this. Why is it so? How can I solve this? Any suggestion much appreciated.

tpsaitwal
  • 422
  • 1
  • 5
  • 23

1 Answers1

2

Look at your path variable in the Java class. /input translates as http://localhost:8001/input/post which may not be what you want.

You are also re-declaring the @Path variable twice in your Java file. Once before the InputResponse class definition and again inside the InputResponse class itself.

  • 1
    And also look at your ajax call's url value. Single dot slash is the working directory. Here's a link that explains path differences between ./ and ../ http://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location – lawrence thorne Sep 16 '15 at 04:56
  • 1
    See at my web.xml, I have given url-pattern as /rest/*, and the same thing as a whole is working fine for me with a GET ajax request.@lawrence thorne – tpsaitwal Sep 16 '15 at 05:19