java api在处理后返回空json文件
我有一个spring API,需要对收到的json文件进行处理,并将处理结果返回到另一个url
我这里有两个问题:
1/我需要读取我收到的JSONObject,因为当我发送一个json文件时,我会看到一个空的json文件
2/我需要返回一个JSONObject。我发送的是一个json文件,其中包含{"empty":true}
首先,图书馆组织。json是强加给我的,所以我不能使用任何其他库(我使用的是org.json.simple,它对我有用,但后来他们使用了org.json库)。我试图将接收到的对象更改为字符串并执行该过程。它可以工作,但我更喜欢接收JSONObject。 顺便说一句,我查了一下互联网和stackoverflow,但没有找到解决办法
以下是主要代码
@Service
public class DataService {
@Value("${my.config.api.atos.user}")
private String user;
@Value("${my.config.api.atos.password}")
private String password;
@Value("${my.config.folder}")
private String path;
//It works with org.json.simple library.
// I would prefer that the variable is a JSONObject
public static String processData(String str) {
JSONObject jsonObject = new JSONObject(str);
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
Data data = new Data();
try {
data = mapper.readValue(jsonObject.toString(), Data.class);
} catch (IOException e) {
e.printStackTrace();
}
List<DataJson> timeserie = data.getData();
List<Double> values = new ArrayList<Double>();
DataJson inter;
for (int i = 0; i<timeserie.size(); i++){
inter = timeserie.get(i);
values.add(inter.getValue());
}
System.out.println(values);
int EmbeddingDimension;
EmbeddingDimension = data.getEmbeddingDimension();
data.setResult(DynamicProperties.PermEn(values, EmbeddingDimension));
String url = "http://localhost:8080/crosscpp/toolbox/test";
OkHttpClient client = new OkHttpClient();
ObjectMapper objectMapper = new ObjectMapper();
RequestBody body = null;
try {
body = RequestBody.create(
MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(data));
System.out.println(objectMapper.writeValueAsString(data));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
try {
Response response = call.execute();
String result = response.body().string();
System.out.println(result);
JSONObject json = new JSONObject(result);
System.out.println(json);
return result;
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
以下是我发送的json对象:
{
"inputAeonSubscriptionUrl": "xxxx",
"outputAeonPublicationUrl": "xxxx",
"EmbeddingDimension": 3,
"offerId": "xxxxxx",
"measurement-channel-id": "1",
"id": "xxxxxx",
"submissionDate": {
"min": "2019-04-09",
"max": "2019-05-07"
},
"travelDates": {
"min": "2019-05-13",
"max": "2019-05-17"
},
"travelledDuration": {
"min": 1,
"max": 2
},
"geoBoundingBox": {
"latitude-max": 51.507561,
"latitude-min": 51.497715,
"longitude-min": 7.482349,
"longitude-max": 7.500885
},
"data": [{
"value": 1,
"timestamp": "2019-04-09"
},
{
"value": 3,
"timestamp": "2019-04-09"
},
{
"value": 2,
"timestamp": "2019-04-09"
},
{
"value": 1,
"timestamp": "2019-04-09"
},
{
"value": 2,
"timestamp": "2019-04-10"
},
{
"value": 3,
"timestamp": "2019-04-10"
},
{
"value": 2,
"timestamp": "2019-04-10"
}
]
}
下面是DataController:
@RestController
public class DataController {
private final DataService dataservice;
@Autowired
public DataController(DataService dataservice) {
this.dataservice = dataservice;
}
@RequestMapping(method=RequestMethod.POST, value="/test")
public JSONObject getAllDataT(@RequestBody JSONObject data) {
return data;
}
@RequestMapping(method=RequestMethod.POST, value="/testjson")
public JSONObject getAllDataT2(@RequestBody JSONObject json) {
return json;
}
@RequestMapping(method = RequestMethod.POST, value = "/timeseries"/*, produces = "application/json"*/)
public JSONObject proccessAtosData(@RequestBody String data) {
return dataservice.processData(data);
}
@RequestMapping(method = RequestMethod.POST, value = "/save")
public void saveAtosData(@RequestBody JSONObject json) {
dataservice.savefile(json);
}
}
最后是数据类:
public class Data {
private String inputAeonSubscriptionUrl;
private String outputAeonPublicationUrl;
private int embeddingDimension;
private String offerId;
private String measurement_channel_id;
private String id;
private SubDate submissionDate;
private TravelDates travelDates;
private TravelDuration travelledDuration;
private GeoBoundingBox geoBoundingBox;
private List<DataJson> data;
private double result;
//private String model;
public Data(){
}
public Data(@JsonProperty("inputAeonSubscriptionUrl") String inputAeonSubscriptionUrl, @JsonProperty("outputAeonPublicationUrl") String outputAeonPublicationUrl,
@JsonProperty("EmbeddingDimension") int embeddingDimension, @JsonProperty("offerId") String offerId,
@JsonProperty("measurement-channel-id") String measurement_channel_id, @JsonProperty("id") String id,
@JsonProperty("submissionDate") SubDate submissionDate, @JsonProperty("travelDates") TravelDates travelDates,
@JsonProperty("travelledDuration") TravelDuration travelledDuration, @JsonProperty("geoBoundingBox") GeoBoundingBox geoBoundingBox,
@JsonProperty("data") List<DataJson> data/*, String model*/) {
this.inputAeonSubscriptionUrl = inputAeonSubscriptionUrl;
this.outputAeonPublicationUrl = outputAeonPublicationUrl;
this.embeddingDimension = embeddingDimension;
this.offerId = offerId;
this.measurement_channel_id = measurement_channel_id;
this.id = id;
this.submissionDate = submissionDate;
this.travelDates = travelDates;
this.travelledDuration = travelledDuration;
this.geoBoundingBox = geoBoundingBox;
this.data = data;
//this.model = model;
}
// getters amd setters
}
如果你需要我提供更多信息,请告诉我,我会尽力发布
共 (0) 个答案