Java 我需要在tomcat中使用@POST with jersey的地图吗

Java 我需要在tomcat中使用@POST with jersey的地图吗,java,json,rest,tomcat,jersey,Java,Json,Rest,Tomcat,Jersey,我试图在tomcat服务器中创建一个带有Jersey的RESTAPI,我的应用程序从其他服务收集输入(没有数据库)。 我创建了以下POJO: //this a pojo for parameters the application takes in the @POST request, a location lat/long @XmlRootElement public class DestLatLong { private String lat; private String

我试图在tomcat服务器中创建一个带有Jersey的RESTAPI,我的应用程序从其他服务收集输入(没有数据库)。 我创建了以下POJO:

//this a pojo for parameters the application takes in the @POST request, a location lat/long

@XmlRootElement
public class DestLatLong {
    private String lat;
    private String lng;

    public DestLatLong(){}

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

}
以下是将包含在响应中的值,即到达给定位置的票价:

public class DestPriceResponse {

    public DestPriceResponse() {
    }
    public String airport;
    public String currency;
    public String price;
    public String getAirport() {
        return airport;
    }
    public void setAirport(String airport) {
        this.airport = airport;
    }
    public String getCurrency() {
        return currency;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
}
我是否需要在这里使用映射来将pojo正确映射到JSON?或者这个物体够了吗

下面是应处理请求并返回响应的类:

public class DestinationResource {


// Will map the resource to the URL todos
@Path("/api")
public class TodosResource {

    // Allows to insert contextual objects into the class,
    // e.g. ServletContext, Request, Response, UriInfo
    @Context
    UriInfo uriInfo;
    @Context
    Request request;


    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public DestPriceResponse getDestPrice(String lat, String lng) {
        DestPriceResponse priceResponse = new DestPriceResponse();

        NearestAirports nearest = new NearestAirports();
        nearest.parseNearestAirports(lat, lng);
        return priceResponse;
    }
我使用的POST请求是:

http://localhost:8080/webapi/rest/api?lat=60&lng=0

标题有:

Content-Type=application/json

但我一直得到:

HTTP状态404–未找到

类型状态报告

找不到消息

说明源服务器未找到当前表示形式 对于目标资源或不愿意披露其存在

ApacheTomcat/8.5.23

我的设置包括:

rootProject.name='webapi'

和webxml:

<?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>webapi</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>webapi</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

webapi
泽西岛休息服务
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
webapi
1.
泽西岛休息服务
/休息/*

出了什么问题,我如何才能最好地调试它?

此调用
http://localhost:8080/webapi/rest/api?lat=60&lng=0
假设您正在公开HTTP
GET
方法,该方法在
getDestPrice()
方法参数(lat和lng)上使用
@QueryParam()
注释,如下所示:

@GET
@Produces(MediaType.APPLICATION_JSON)
public DestPriceResponse getDestPrice(@QueryParam("lat") String lat, @QueryParam("lng") String lng) {
如果要使用接受数据对象的
POST
方法,应将方法签名更改为以下内容:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public DestPriceResponse getDestPrice(DestLatLong longLat) {
  • 更改HTTP调用并使用任何HttpClient(例如chrome扩展名)
  • 添加
    内容类型的头:application/json
    并传递json 描述
    DestLatLong
    对象
  • Jersey
    将在运行时调用api时为您创建此对象

您收到错误404,因为您在
/
上没有HTTP GET端点(您定义了POST)

谢谢@Gal的回答。我发现GET是一种更简单的方法,所以我遵循了上面的示例。然而,当我使用我的httpClient向端点发出GET请求时,仍然会得到一个“未找到”的消息。这会是一个路由问题吗?有什么简单的方法可以调试它并找到根本原因吗?