gpt4 book ai didi

java - 动态更新 HashMap 中值的特定部分

转载 作者:行者123 更新时间:2023-12-02 02:09:53 25 4
gpt4 key购买 nike

我有一个带有 HTTP post 请求处理程序的 Spring Boot 应用程序。它接受我解析的有效负载并输出 JSON。我已经处理过它需要接受某些参数的有效负载(18)。

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;




@Validated
@RestController
public class MockController {


@Autowired
MockConfig mockconfig;

private static final Logger LOGGER = LoggerFactory.getLogger(MockController.class);




@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "hello!";
}
String[] parse;
@PostMapping(value = "/")
public String payloader(@RequestBody String params ) throws IOException{
LOGGER.debug("code is hitting");
parse = params.split("\\|");
String key;
String value;
String dyn;
Map<String, String> predictionFeatureMap = mockconfig.getPredictionFeatureMap();

if(parse.length!=18) {

key = "Not_enough_parameters";
value = predictionFeatureMap.get(key);
Map<?, ?> resultJsonObj = new Gson().fromJson(value, Map.class);

}
else {
key = params;
value = predictionFeatureMap.get(key);
}
return value;
}
}

我的配置类是我从文件获取输入和输出并将它们放入 HashMap 中的地方。

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.springframework.context.annotation.Configuration;

@Configuration
public class MockConfig {
private Map<String, String> predictionFeatureMap = new HashMap<String, String>();

public Map<String,String> getPredictionFeatureMap() throws IOException {
return predictionFeatureMap;
}

public MockConfig() throws IOException {
init();
}

private Map<String, String> init() throws IOException {
Scanner sc = new Scanner (new File("src/test/resources/Payload1.txt"));

int counter = 1;
String key = "";
while (sc.hasNextLine()) {
if(counter % 2 != 0) {
key = sc.nextLine();
}else {
predictionFeatureMap.put(key, sc.nextLine());
}
counter++;
}
sc.close();
return predictionFeatureMap;
}




}

这是我正在尝试专门使用的文件中的键和值。

Not_enough_parameters
{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting: 18, actual:8", "version": "0.97", "coreName": "Patient_Responsibility"}

(JSON字符串是参数过多或过少的响应...参数长度应为18。)

输入示例:

ncs|56-2629193|1972-03-28|20190218|77067|6208|3209440|self|-123|-123|-123|0.0|0.0|0.0|0.0|0.0|0.0|0.0

此输入将通过,因为它有 18 个参数...

我想做的是,如果用户 curl 例如 5 个参数

ncs|56-2629193|1972-03-28|20190218|77067

我希望值(JSON 错误消息)的“实际”字段更新为:

{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting: 18, actual:5", "version": "0.97", "coreName": "Patient_Responsibility"}

无需将其硬编码到 txt 文件或 HashMap 中...

我尝试获取字符串的索引并用 parse.length() 替换“8”字符并将其转换为字符,但它只给了我这个输出:

{"status": false, "errorMessage": "Payload has incorrect amount of parts: expecting:1 actual:", "version": "0.97", "coreName": "Nulogix_Patient_Responsibility"}

如何解析或索引 JSON 来更新此值?或者有没有 hashmap 方法来处理这个问题?

最佳答案

使用框架时,通常使用框架处理错误的方式来处理错误。

为了处理 Spring Boot 中的错误,您通常使用 Controller 建议来帮助处理错误。这是通过使用 @ControllerAdvice 注解一个类来创建的。

在那里您可以捕获抛出的异常并构建将返回给调用客户端的响应。

@PostMapping(value = "/")
public String payloader(@RequestBody String params ) throws IOException{
LOGGER.debug("code is hitting");
parse = params.split("\\|");
String key;
String value;
String dyn;
Map<String, String> predictionFeatureMap = mockconfig.getPredictionFeatureMap();

if(parse.length!=18) {

// Here you throw a custom runtime exception and pass what
// parameters you want that will help you build your error
// message you are passing to your client.

final String errorMessage = String.format(
"Payload has incorrect amount of parts: expecting:%d actual:%d",
predictionFeatureMap.size(),
parse.length);

throw new MyException(errorMessage);

}
else {
key = params;
value = predictionFeatureMap.get(key);
}
return value;
}

然后在 Controller 建议类中

@ControllerAdvice
public class Foo extends ResponseEntityExceptionHandler {

@ExceptionHandler(MyException.class)
public ResponseEntity<MyCustomErrorBody> handleControllerException(HttpServletRequest request, MyException ex) {

// Build your error response here and return it.
// Create a class that will represent the json error body
// and pass it as body and jackson will deserialize it for
// you into json automatically.

final MyCustomErrorBody body = new MyCustomErrorBody(false, ex.getMessage(), "0.97", "Patient_Responsibility")

return ResponseEntity.unprocessableEntity().body(myCustomErrorBody).build();
}
}


public class MyCustomErrorBody {

private boolean status;
private String errorMessage;
private String version;
private String coreName;

// constructor and getters setters ommitted

}

有关 Spring Boot 错误处理的链接:

Official spring boot documentation

Baeldung exception-handling-for-rest-with-spring

关于java - 动态更新 HashMap 中值的特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com