【问题标题】:Invoking rest service with sql.timestamp variable in model object for @RequestBody在 @RequestBody 的模型对象中使用 sql.timestamp 变量调用休息服务
【发布时间】:2014-10-28 21:26:12
【问题描述】:

如果我将变量 firstDT 从 Timestamp 更改为 String,然后在 get 和 set 方法中从 Timestamp 转换为 String 或反之,我知道如何使下面的代码运行。我想知道是否可以使下面的模型与 Timestamp 变量一起使用。

//Client:
HttpPost postRequest = new HttpPost("http://localhost:8080/my_app/log/Mas60010");
ObjectMapper mapper = new ObjectMapper();
String json = "";

Map<String, String> map = new HashMap<String, String>();
map.put("subCd", "A");

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
map.put("firstDT", "/Date(" + format.format(new Date()) + ")/");

map.put("messageFormat", "AFMAMXAS2CAPCISIMFMDSQSTSTRVIS");

json = mapper.writeValueAsString(map);

StringEntity input = new StringEntity(json);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = httpClient.execute(postRequest);

//Rest Web Service:
@RequestMapping(value="Mas60010", method=RequestMethod.POST)
@ResponseBody
public String getFirst(@RequestBody Mas60010 mas60010) {
    return "I only reach here if firstDT is String ";
}

//Model
import java.sql.Timestamp;
public class Mas60010 {
       private String subCd;
       private Timestamp firstDT; // the doubt root is here
       private String messageFormat;
       public String getSubCd() {
              return subCd;
       }

       public void setSubCd(String subCd) {
              this.subCd = subCd;
       }

       public Timestamp getFirstDT() {
              return firstDT;
       }

       public void setFirstDT(Timestamp firstDT) {
              this.firstDT = firstDT;
       }

       public String getMessageFormat() {
              return messageFormat;
       }

       public void setMessageFormat(String messageFormat) {
              this.messageFormat = messageFormat;
       }

       @Override
       public String toString() {
         return "ok";
       }
}

【问题讨论】:

  • 嗨,我还在寻找这个问题的答案。
  • 没人能帮我解决这个问题吗?
  • 嗨..你找到解决方案了吗..我也面临同样的问题。你的解决方案会帮助我。请分享..

标签: java json rest post


【解决方案1】:

我无法具体回答这个问题,但我只是尝试使用具有类型为java.sql.Timestamp 的字段变量的对象进行 REST 调用。我可以通过使用它的超类java.util.Date 来解决这个问题。它不起作用的原因是Timestamp 没有空的构造函数并且Date 没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多