如何为具有相同路径和 producing/consuming 相同媒体类型的两种不同方法设计 Jax - RS?

How to Design Jax - RS for two different methods with same path and producing/consuming same media type?

我想用两种方法设计一个 Post 服务。方法名称会有所不同。这两个方法将采用不同的对象进行调用。但是 Path 和 Production/Consumption 媒体类型必须相同。怎么做?请在下面找到此代码。它给出了异常,如“资源模型对 HTTP 方法 POST 和输入 mime 类型有不明确的(子)资源方法,如 Java 方法中的“@Consumes”和“@Produces”注释所定义" 在部署到 weblogic 时。


package com.adac.rest.service;

import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import com.adac.rest.model.Response;
import com.adac.rest.model.SalesData;
import com.adac.rest.model.Tax;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


@Path("/rest")
public class StageDataLoadingService {

    @POST
    @Path("/dataloading")
    @Consumes("application/json")
    @Produces("application/json")
    public String resourceMethodPUTSalesData(SalesData sd) throws Exception  { 

        System.out.println("This method called with Sales Data");

        Response resp = new Response();
        resp.setStatuscode("0");
        resp.setStatusmessage("Success");

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        String json_resp = mapper.writeValueAsString(resp);



        String str1 = mapper.writeValueAsString(sd);
        System.out.println(str1);

       //String sd1 = mapper.writeValueAsString(sd);    
      //System.out.println(sd1);
       // JsonNode jsonNodeRoot = mapper.readTree(json_resp);

        return json_resp;

    }


      @POST

      @Path("/dataloading")

      @Consumes("application/json")

      @Produces("application/json") public String resourceMethodPUTTax(Tax tax)
      throws Exception {

      System.out.println("This method called with Tax");

      Response resp = new Response(); resp.setStatuscode("0");
      resp.setStatusmessage("Success");

      ObjectMapper mapper = new ObjectMapper();
      mapper.enable(SerializationFeature.INDENT_OUTPUT); String json_resp =
      mapper.writeValueAsString(resp);



      String str1 = mapper.writeValueAsString(tax); 
      System.out.println(str1);


      return json_resp;

      }

}

另外,建议 web.xml 和 weblogic.xml 配置用于您建议的设计。

目前 web.xml 是:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>ADACStageDataLoadingService</display-name>
    <servlet>
        <servlet-name>rest.application.config.ApplicationConfig</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>rest.application.config.ApplicationConfig</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>


和weblogic.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
    xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
    <wls:weblogic-version>12.2.1.3</wls:weblogic-version>
    <wls:context-root>ADACStageDataLoadingService</wls:context-root>
    <wls:library-ref>
        <wls:library-name>jax-rs</wls:library-name>
        <wls:specification-version>2.0</wls:specification-version>
        <wls:exact-match>false</wls:exact-match>
    </wls:library-ref>
</wls:weblogic-web-app>

当您开发 api 我建议您遵循其余的最佳实践:

  • 如果要插入资源,请使用 POST 方法并使用以下 @Path:/SalesData/{resourceId}(您可以省略 "dataloading",因为它隐含在 POST 方法中
  • 如果您想更新资源,请使用具有相同路径的 PUT 方法:/SalesData/{resourceId}
  • 如果要删除资源,请使用 DELETE 方法