json - 如果方法 validateOpco 返回 null 地图对象,我的 Rest Controller 类必须显示 Resource Not Found Exception
问题描述
我的服务等级:
package com.techwave.JsonObjectToHashmap.service;
import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONArray;
import org.json.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.json.simple.parser.*;
import com.techwave.JsonObjectToHashmap.model.Data;
import com.techwave.JsonObjectToHashmap.model.OpcoData;
@Service
public class CreditRiskService
{
//public static String original1 = "";
public static Map<String,String> mapForInputJson;
public static String original2 = "";
public static Map<String,String> mapForOpcJson;
public static String opcoArray ="";
public static Map<String,String> finalOpcoJson;
public ArrayList<String> totalOpcosInOpcoJson;
public static Map<String,String> equalOpcoJson;
public HashMap<String,String> validateOpco() throws JSONException, FileNotFoundException
{
try
{
File file1 = new File(getClass().getClassLoader().getResource("CredirRisk_corainputtoIIB.json").getFile());
if(file1.exists())
{
JSONTokener jsonDataFile1;
JSONObject jsonObject1;
jsonDataFile1 = new JSONTokener(new FileInputStream(file1));
jsonObject1 = new JSONObject(jsonDataFile1);
if(!JSONObject.NULL.equals(jsonObject1))
{
String jsonObjToString1 = (String)jsonObject1.toString();
//original1=jsonObjToString1;
mapForInputJson = new HashMap<String,String>();
try
{
Iterator<?> keys = jsonObject1.keys();
while (keys.hasNext())
{
String key = (String) keys.next();
String value = jsonObject1.getString(key);
//if(((mapForInputJson.get("target")) != null) || ((mapForInputJson.get("target").length()==4)))
mapForInputJson.put(key, value);
}
System.out.println("mapForInputJson:" +mapForInputJson);
System.out.println("****");
System.out.println (mapForInputJson.get("opcode"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
else
{
System.out.println("The Cora Input Json File Does Not Exist");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
File file2 = new File(getClass().getClassLoader().getResource("opco.json").getFile());
if(file2.exists())
{
JSONTokener jsonDataFile2;
JSONObject jsonObject2;
jsonDataFile2 = new JSONTokener(new FileInputStream(file2));
jsonObject2 = new JSONObject(jsonDataFile2);
if(!JSONObject.NULL.equals(jsonObject2))
{
JSONArray jsonArray = (JSONArray) jsonObject2.get("opcodeDesc");
System.out.println("Opcode Description Details: ");
for(int i=0;i<jsonArray.length();i++)
{
JSONObject opcodeDescriptionDetails= (JSONObject)jsonArray.get(i);
if (!JSONObject.NULL.equals(opcodeDescriptionDetails))
{
String opcoarray = (String)opcodeDescriptionDetails.toString();
opcoArray = opcoarray;
}
HashMap<String, String> map2 = new HashMap<String, String>();
JSONObject jObject = new JSONObject(opcoArray);
Iterator<?> keys = jObject.keys();
while( keys.hasNext() )
{
String key = (String)keys.next();
String value = jObject.getString(key);
map2.put(key, value);
}
System.out.println("MAP2 OPCODES:" +map2.get("opcode"));
for(int k=0;k<map2.size();k++)
{
finalOpcoJson=map2;
}
System.out.println("TikTok:" +finalOpcoJson.get("opcode"));
System.out.println("opcode from input json file:" +mapForInputJson.get("opcode"));
System.out.println((mapForInputJson.get("opcode")).equals (finalOpcoJson.get("opcode")));
if((mapForInputJson.get("opcode")).equals(finalOpcoJson.get("opcode")))
{
HashMap<String,String> map3 = new HashMap<String,String>();
map3.putAll(finalOpcoJson);
equalOpcoJson = map3;
}
}
System.out.println("equalOpcoJson:" +equalOpcoJson );
//System.out.println (mapForInputJson.get("opcode"));
//System.out.println("finalOpcoJson values:" +finalOpcoJson.get("opcode"));
//System.out.println("List of Opco in OpcoJson file:" +totalOpcosInOpcoJson);
//System.out.println("Ping Pong:" +finalOpcoJson.get("opcode"));
}
//return (HashMap<String, String>) mapForInputJson;
}
else
{
System.out.println("The Opco Json File Does Not Exist");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return (HashMap<String, String>) equalOpcoJson;
}
}
我的休息控制器类:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.techwave.JsonObjectToHashmap.exception.ResourceNotFoundException;
import com.techwave.JsonObjectToHashmap.service.CreditRiskService;
@RestController
public class CreditRiskController
{
@Autowired
CreditRiskService CreditRiskService;
@GetMapping("/creditRisk")
private HashMap<String,String> validateOpco1() throws ParseException, IOException, ResourceNotFoundException
{
return CreditRiskService.validateOpco();
}
/*@GetMapping("/jsonObjectToMap")
private HashMap<String, String> getAllValidationsH() throws FileNotFoundException, ParseException
{
CreditRiskService.getMatchingOpco();
return null;
}*/
}
在上面的 CreditRiskController 类中,如果方法 validateOpco1() 返回 null HashMap 对象,方法 validateOpco1() 必须抛出或显示 ResourceNotFoundException。你能建议我吗?
我的 Service 类和 RestController 类如上。