在@RequestBody的模型对象中使用sql.timestamp变量调用rest服务

时间:2014-10-28 21:26:13

标签: java json rest post

我知道如果我将变量firstDT从Timestamp更改为String,然后将Timestamp从Timestamp转换为String,并在get和set方法内部转换为副本,我知道如何使下面的代码运行。我想知道是否可以使下面的模型使用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";
       }
}

1 个答案:

答案 0 :(得分:0)

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