0

I cant get my Ajax post to work with Resteasy. I get no error in java, but in browser i get this :

event.returnValue is deprecated. Please use the standard event.preventDefault() instead. 
OPTIONS https://localhost:8081/TestResteasy/rest/servlet/post  

Can anyone tell me what i did wrong? If i type the link directly,the service works just fine.

js file

$(document).ready(function() {
$("button").click(function() {
    var dataT = {
        "name" : "Daniel",
        "password" : "1234"
    };

    $.ajax({
        type : "POST",
        data : JSON.stringify(dataT),
        dataType : "json",
        contentType : "application/json",
        url : "https://localhost:8081/TestResteasy/rest/servlet/post",
        success : function(data) {
            alert(data);
        }
    });
});

});

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>JAXRS-RESTEasy</display-name>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<!-- Auto scan REST service -->
<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<!-- this should be the same URL pattern as the servlet-mapping property -->
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

the service

@Path("/servlet")
public class RESTEasyHelloWorldService {

@GET
@Path("/get/{pathParameter}")
public Response responseMsg(
        @PathParam("pathParameter") String pathParameter,
        @DefaultValue("Nothing to say") @QueryParam("queryParameter") String queryParameter) {

    String response = "Hello from: " + pathParameter + " : "
            + queryParameter;

    return Response.status(200).entity(response).build();
}

@POST
@Path("/post")
@Consumes("application/json")
public Response createProductInJSON(Product product) {

    String result = "Product created : " + product.getName();
    System.out.println(product.getName());
    return Response.status(201).entity(result).build();

}

}

the object

package com.javacodegeeks.enterprise.rest.domain;

public class Product {

String name;
String password;

public Product(String name, String password) {
    this.name = name;
    this.password = password;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@Override
public String toString() {
    return "Product [name=" + name + ", qty=" + password + "]";
}

}

pom.xml

    <dependencies>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.0.2.Final</version>
    </dependency>
</dependencies>
user2837742
  • 55
  • 1
  • 7

1 Answers1

0

Sorry for late reply but may be this will be helpful in future for others

Step 1:Add the runtime support for RESTEasy JSAPI

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jsapi</artifactId>
    <version>2.3.1.GA</version>
</dependency>

Step 2: Add JSAPI servlet mapping in web.xml

  <servlet>
 <servlet-name>RESTEasy JSAPI</servlet-name>
 <servlet-class>org.jboss.resteasy.jsapi.JSAPIServlet</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>RESTEasy JSAPI</servlet-name>
 <url-pattern>/rest-js</url-pattern>
</servlet-mapping>
for more detail kindly visit the link 

http://docs.jboss.org/resteasy/docs/2.0.0.GA/userguide/html/AJAX_Client.html

MSR
  • 535
  • 7
  • 19