使用webtarget调用客户端的休息请求。但是其余资源的参数未映射到客户端模型类参数

时间:2018-08-01 09:37:26

标签: java rest jax-rs jersey-2.0

我正在使用Jersey 2.0通过JAX-RS客户端调用休息请求

User request = new User("1","ABC");

webTarget = client.target(strRestURL).path("Users");

Invocation.Builder invocationBuilder =  webTarget.request(MediaType.APPLICATION_XML); 
Response setUserResponse = invocationBuilder.post(Entity.entity(request, MediaType.APPLICATION_XML)); 
String resp = setUserResponse.readEntity(String.class);

客户端的模型类如下:

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "request")
public class User   {
    private static final long serialVersionUID = 1L;

    private String id;
    private String Name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id= id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        this.Name= name;
    }
}

其余的WS实现是:

@POST
@Path("/User")
@Consumes({MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_XML})
public HSServiceResponse setUsers(final User model, @Context HttpServletRequest request) throws IllegalArgumentException, IllegalAccessException {

资源用户服务器端:

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.NONE)
public class User   {
    private static final long serialVersionUID = 1L;
    @XmlElement
    private String id;
    @XmlElement
    private String Name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id= id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        this.Name= name;
    }
}

在这里,服务器端资源类的Name属性在接收到发布请求时将接收值为null。但是id参数已正确映射。

如果在资源类服务器端实现中将参数Name更改为name。它将完美运行。但是,我的要求是将其余参数保留为仅Name(“ N”大写)而不是name(“ n” small)。

如何实现从Jersey客户端发送请求以将Nameid一起映射。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用import pyspark.sql.functions as F from pyspark.ml import Pipeline, Transformer from pyspark.sql import DataFrame from pyspark.ml.classification import DecisionTreeClassifier from pyspark.ml.feature import VectorAssembler import pandas as pd data = pd.DataFrame({ 'ball': [0, 1, 2, 3], 'keep': [4, 5, 6, 7], 'hall': [8, 9, 10, 11], 'fall': [12, 13, 14, 15], 'mall': [16, 17, 18, 10], 'label': [21, 31, 41, 51] }) df = spark.createDataFrame(data) input_cols = ['ball', 'keep', 'hall', 'fall'] assembler = VectorAssembler( inputCols=input_cols, outputCol='features') dtc = DecisionTreeClassifier(featuresCol='features', labelCol='label') pipeline = Pipeline(stages=[assembler, dtc]).fit(df) transformed_pipeline = pipeline.transform(df) ml_pipeline = pipeline.stages[1] string = ml_pipeline.toDebugString for i, feat in enumerate(input_cols): string = string.replace('feature ' + str(i), feat) print(string) 批注的name属性。

DecisionTreeClassificationModel (uid=DecisionTreeClassifier_4eb084167f2ed4b671e8) of depth 3 with 7 nodes
  If (ball <= 0.0)
   Predict: 21.0
  Else (ball > 0.0)
   If (ball <= 1.0)
    Predict: 31.0
   Else (ball > 1.0)
    If (ball <= 2.0)
     Predict: 41.0
    Else (ball > 2.0)
     Predict: 51.0

这使您可以更改XML元素名称,而不必在Java代码中使用时髦的变量名称。

更新

对于客户端上的@XmlElement,XML元素的名称由Java bean属性获得,即class UserServer { @XmlElement(name="Name") private String name; } ,除去User并小写第一个字母。这就是JavaBean约定的工作方式。如果要更改此设置,则应在getName方法上添加get

@XmlElement(name="Name")