- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我研究 StackOverflow 好几天了,浏览了这些链接 1和 2和 3但一切都无济于事。
我是 Java Web 应用程序的新手。我在我的项目中使用 Tomcat 9.0.4 和 Jersey 1.18。我想将 4 个参数作为 4 个点传递给用 JAX-RS 编写的 post Web 服务,并返回面积和周长作为响应。这是我的服务器端网络服务:
@path("/geo")
public class geonodes{
static double area(double x1,double y1,double x2,double y2){
//do area calculation
}
static double perimeter(double x1,double y1,double x2,double y2){
//do perimeter calculation
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response calculate(@QuerParam("x1") Double x1,@QuerParam("y1") Double y1,@QuerParam("x2") Double x2,@QuerParam("y2") Double y2){
JSONObject obj = new JSONObject();
obj.put("area",area(x1,y1,x2,y2));
obj.put("perimeter",perimeter(x1,y1,x2,y2));
result = JSONObject.toJSONString(obj);
return Response.status(201).entity(result).build();
}
这是我的 ajax 客户端调用:
$.ajax({
url:'http://localhost:8080/api/geo',
type:'POST',
dataType:'json',
contentType:'application/json',
data:JSON.stringify({x1:22.1,y1:44.19,x2:55.33,y2:49.72}),
success:function(data){console.log(data);},
error:function(errMsg){console.log(errMsg);},
});
我使用一个包含这些 jar 文件的非 Maven 项目:
这是我的 web.xml
<servlet>
<servlet-name>Geo Nodes API</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>example,com.fasterxml.jackson.jaxrs.json</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>Geo Nodes API</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/api/*</url-pattern>
</filter-mapping>
响应始终为空,经过长时间的搜索和谷歌搜索后,我发现参数存在问题,因此作为响应我返回了它们。当我通过 postman 调用地址时,结果正确返回如下: 但是通过ajax或其他系统调用,参数总是空的。
每当我使用 @QueryParam 以外的东西时,即带有 getter 和 setter 的类,如下面的代码, postman 也会收到 415 错误。Coordinats.java 类:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Coordinates {
private Double x1;
private Double y1;
private Double x2;
private Double y2;
public Double getX1() {
return x1;
}
public void setX1(Double x1) {
this.x1 = x1;
}
public Double getY1() {
return y1;
}
public void setY1(Double y1) {
this.y1 = y1;
}
public Double getX2() {
return x2;
}
public void setX2(Double x2) {
this.x2 = x2;
}
public Double getY2() {
return y2;
}
public void setY2(Double y2) {
this.y2 = y2;
}
}
服务器端代码片段:
import model.Coordinates;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("geo")
public class Geo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Coordinates request(Coordinates coordinates){
Double x1 = coordinates.getX1();
Double y1 = coordinates.getY1();
Double x2 = coordinates.getX2();
Double y2 = coordinates.getY2();
return coordinates;
}
}
04-Dec-2020 05:53:14.771 SEVERE [http-nio-8080-exec-7] com.sun.jersey.spi.container.ContainerRequest.getEntity A message body reader for Java class model.Coordinates, and Java type class model.Coordinates, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
我添加了所有需要的 jar 文件,现在我很困惑哪里出了问题!?当我传递一个对象时,我得到一个 415 错误,当我传递 @QueryParam 时, postman 调用可以访问参数,但 ajax 调用无法访问!!!
最佳答案
如果你想发送JSON,那么你这里的是正确的
@Path("geo")
public class Geo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Coordinates request(Coordinates coordinates){
Double x1 = coordinates.getX1();
Double y1 = coordinates.getY1();
Double x2 = coordinates.getX2();
Double y2 = coordinates.getY2();
return coordinates;
}
}
你有所有的 jackson jar 。您应该删除 web.xml 中的 POJOMappingFeature 并删除 jersey-json
jar,因为这可能会导致一些冲突。
然后在web.xml中添加如下内容
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
example,
com.fasterxml.jackson.jaxrs.json
</param-value>
</init-param>
这将告诉 Jersey 扫描 Jackson 包以查找 JSON 提供程序。
现在在您的 Postman 调用中,不要使用“params”,因为它不会创建 JSON。您应该使用“正文”选项卡,然后手动输入您要发送的 JSON
{
"x1": 1.1,
"y1": 2.2,
"x2": 3.3,
"y2": 4.4
}
然后将 Content-Type
header 设置为 application/json
完成所有这些后,它应该可以工作了。顺便说一句,我强烈建议您学习使用依赖管理器,Maven 或 Gradle。尝试添加 jar 时,您经常会错过添加该 jar 所依赖的 jar,这将导致该 jar 无法工作。
关于java - 在 jax-rs 中传递 post 数据参数并通过 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65125854/
来自 JSR-339: For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups oth
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
有没有办法获得java.lang.reflect.Method为给定的 @Path 调用的方法(用 HttpServletRequest 注释) ? 这是我的用例:我在 Java EE 中 Filte
我接到了一家公司的任务,该任务向我发送了一台已完成所有设置的虚拟机。任务是我必须创建一个 API 来从数据库中检索人员详细信息并显示它。 问题是,当我运行应用程序时,服务器返回一个包含 hello w
我有一个 POST 方法调用,它接受很少的表单参数。我希望 JAX-RS 能够处理不存在特定表单参数的情况。 示例: @POST @Produces (MediaType.APPLICATION_JS
我有 JAX-RS 网络应用程序,我想记录从获取请求到响应的时间量。在带有 servlet 过滤器的 Spring Boot 中很容易。但是我的应用程序中的过滤器无法正常工作: @Provider p
使用以下网址。 http://doma.in/context/resource/some/.../undefined 我想获取 ../resource 之后的路径名,即 /some/.../undef
Closed. This question is off-topic。它当前不接受答案。
我花了几个小时在嵌入式 Jetty 9.1.0.v20131115 和 RESTEasy 3.0.5.Final 中安装自定义登录服务。我的登录服务将在数据库中查找用户并为他们分配角色。它看起来像这样
RESOURCE_STALLS.RS的说明Intel Broadwell 的硬件性能事件如下: This event counts stall cycles caused by absence of
我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。 @GET public Response getUserImage() { byte[] image =new
(特别是 RESTeasy) 具有如下方法签名会很好(对于单个文件): public void upload(@FormParam("name") ..., @FormParam("file") fi
RESTEasy (JAX-RS) 允许通过子资源进行动态调度。例如: POST /customers/create {"name":"Smith","country":"jp"} 我们可以有一个根资
假设有一个 ResultSet rs 和 n 对象。 这段代码: while(rs.next()) { // do something on rs } 在算法上等于此代码(即两者给出相同的结果)
我正在使用 Visual Studio 2005 和 mysql 4.0,我有一个表,其中包含一个字段(十进制 16,4),用于存储发票金额和另一个表,其中包含对该发票的付款(这些字段也是十进制的)
我正在使用 JAX-RS、Microprofile 和 Payara 5 构建 REST 服务。我的方法返回类型为 Response 的对象。响应本身包含一个 MyClass 列表。实现看起来像这样:
我已经使用 Mock MVC 成功地为 Spring MVC REST 服务构建了无需部署的集成测试框架。我正在努力对 JAX-RS 服务做同样的事情。 REST Assured 看起来很有希望,但它
我有一个使用JAX-RS 2.0的Jersey实现的Java应用程序,并且我想在客户端启用gzip压缩。服务器已启用它,并且通过在Chrome中的开发人员工具中的“大小/内容”中查找客户端正在使用的特
我正在尝试与 Tomcat7 一起使用 RESTful Web 服务 (JAX-RS)。我尝试了 3 种不同的实现(Jersey、RESTeasy 和 ReSTLet),但都没有成功。这应该很容易,但
swagger-api有什么区别和 JAX-RS ? 是swagger-api仅用于文档? (例如 @ApiOperation ) 最佳答案 根据 API 文档,JAX-RS是用于 RESTful W
我是一名优秀的程序员,十分优秀!