从Jsp向Rest Controller

时间:2016-06-10 05:51:43

标签: java jquery json rest jsp

我正在尝试将Json数据从Jsp发送到Rest服务控制器.Below是Jsp代码。

function send_data(){
 var pg_registration_id=document.getElementById("pg_registration_id").value;
 var mid=document.getElementById("mid").value;
 var tid=document.getElementById("tid").value;
 var request = {pg_registration_id,mid,tid};
alert("Json Data "+JSON.stringify(request));

 jQuery.ajax({
     url: "/RESTfulExample/rest/makePayment",
     type: "POST",
     data: JSON.stringify(request),  
     dataType: "json",
     beforeSend: function(x) {
       if (x && x.overrideMimeType) {
         x.overrideMimeType("application/j-son;charset=UTF-8");
       }
     },
     success: function(result) {   
     alert('Successs' +data);
     }    
});
}

以下是接受来自Jsp的Post请求的Rest服务控制器。

@Path("/")
public class RequestHandler {
 @POST
    @Path("/makePayment")
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces("application/json")
    public Response merchantAuthention(final MerchantPaymentDetail  request) { 
        MerchantPayment merchantPayment = new MerchantPayment();
        return merchantPayment.merchantPayment(request);
    }

}

下面是Web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.es.rest</param-value>
    </init-param>
    <init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>   
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

下面是Pom.xml

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.7</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.7</version>
</dependency>



<!-- http://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160212</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.1.4.Final</version>
</dependency>

<dependency>
    <groupId>org.hibernate.common</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.4.Final</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

我在Jsp页面上插入警报,警报显示正确的Json数据格式,但在将该Json格式数据发送到Rest Controller时,它显示415。

我是否遗漏了依赖项或Jsp代码中的内容?

1 个答案:

答案 0 :(得分:0)

删除'beforeSend'不需要它,因为你已经使用dataType设置了数据类型,json的形成也是错误的。请参考以下代码。

function send_data(){
 var pg_registration_id=document.getElementById("pg_registration_id").value;
 var mid=document.getElementById("mid").value;
 var tid=document.getElementById("tid").value;
 var request = {pg_registration_id: pg_registration_id, mid: mid, tid: tid};
     alert("Json Data "+JSON.stringify(request)); //

 jQuery.ajax({
     url: "/RESTfulExample/rest/makePayment",
     type: "POST",
     data: request,  
     dataType: "json",
     success: function(result) {   
            alert('Successs' +data);
     }
});
}