我想识别通过请求的请求正文发送的 JSON 中不带引号(作为字符串)插入的数值POST
:
例如,这将是错误的 JSON 格式,因为 age 字段不包含引号:
{
"Student":{
"Name": "John",
"Age": 12
}
}
Run Code Online (Sandbox Code Playgroud)
正确的 JSON 格式是:
{
"Student":{
"Name": "John",
"Age": "12"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我将age
字段的数据类型定义为 a String
,因此"12"
应该是正确的输入。但是,即使12
使用,也不会抛出错误消息。
似乎杰克逊会自动将数值转换为字符串。如何识别数值并返回消息?
这是我迄今为止尝试识别这些数值的方法:
public List<Student> getMultiple(StudentDTO Student) {
if(Student.getAge().getClass()==String.class) {
System.out.println("Age entered correctly as String");
} else{
System.out.println("Please insert age value inside inverted commas");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,"Please insert age value inside inverted commas"
当不带引号插入年龄时,这不会打印到控制台。
我正在使用 JavaParser 库来解析 java 代码并访问 java 代码标记。
以下是我的代码
import java.util.Vector;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.io.FileInputStream;
public class MethodParser {
public static void main(String[] args) throws Exception {
// creates an input stream for the file to be parsed
FileInputStream in = new FileInputStream("F:\\Projects\\Parse.java");
CompilationUnit cu;
try {
// parse the file
cu = JavaParser.parse(in);
} finally {
in.close();
}
cu.accept(new MethodVisitor(), null);
}
private static class MethodVisitor extends VoidVisitorAdapter<Void> …
Run Code Online (Sandbox Code Playgroud)