gpt4 book ai didi

java - 从 Jsp 发送 Json 数据到 Rest Controller 时出现 415(不支持的媒体类型错误)

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:12 25 4
gpt4 key购买 nike

我正在尝试将 Json 数据从 Jsp 发送到 Rest 服务 Controller 。下面是 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 服务 Controller 。

@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 代码中的某些内容?

最佳答案

删除“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);
}
});
}

关于java - 从 Jsp 发送 Json 数据到 Rest Controller 时出现 415(不支持的媒体类型错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com