Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring return自定义消息未按预期返回_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring return自定义消息未按预期返回

Java Spring return自定义消息未按预期返回,java,spring,spring-boot,Java,Spring,Spring Boot,我有以下错误类: private static class GenericError extends Error { private int code; private String message; private Date timestamp; public GenericError(String message, int code, String message1) { super(message);

我有以下错误类:

private static class GenericError extends Error {
        private int code;
        private String message;
        private Date timestamp;

        public GenericError(String message, int code, String message1) {
            super(message);
            this.code = code;
            this.message = message1;
            this.timestamp = new Date();
        }

        public int getCode() {
            return code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        public Date getTimestamp() {
            return timestamp;
        }
    }
    private static class ConfirmResponse extends GenericError {
        private String confirmMessage;
        private String instructions;


        public ConfirmResponse(String message, int code, String message1, String confirmMessage, String instructions) {
            super(message, code, message1);
            this.confirmMessage = confirmMessage;
            this.instructions = instructions;
        }

        public String getConfirmMessage() {
            return confirmMessage;
        }

        public String getInstructions() {
            return instructions;
        }
    }
我是这样使用它的:

@RequestMapping(value="login", method=RequestMethod.POST,  produces = {MediaType.APPLICATION_JSON_VALUE })
    FbLoginResponse login(@RequestBody Body body) throws IOException, InvalidKeySpecException,
            NoSuchAlgorithmException, ConfirmLogin, ClassNotFoundException, ConfirmResponse {
            
            ... 
            ... 
            ... 
            ... 

            ConfirmResponse r = new ConfirmResponse("Error", 111, "An account already exists under this email",
                    "Please confirm your account",
                    "Log in with your prev account");

            throw r;
但我得到的回答是不正确的:

{
  "timestamp": 1467604509746,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "com.userapi.spring.controller.UserController$ConfirmResponse",
  "message": "An account already exists under this email",
  "path": "/user/login/fac

ebook"
}
我还尝试扩展产生相同消息的异常

编辑 我希望能够向客户端返回多个不同的类对象。因此,如果有错误,我可以返回一个

new Error("Error occured")
如果我需要返回自定义消息,我可以这样做

return new CustomMessage("Check your Email!);
当我定义控制器的返回类型(例如

LoginResp login(@RequestBody UserFacebookLoginContext body)

... I can only return LoginResp, I cannot return ErrorResponse, or CustomResponse
基本上,我希望能够返回多种类型的类,例如

 AnyResp login(@RequestBody UserFacebookLoginContext body)
 if error ... return new ErrorResponse("something went wrong");

 if requires-custom ... return new CustomResponse ("Please check your email");

我将给您留下一个示例,说明如何处理不同的对象以返回
ResponseEntity

@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<?> getAll() {
    List<Transferencia> list = transferenciaDAO.getAll();
    if (!list.isEmpty()) {
        return new ResponseEntity<>(list, HttpStatus.OK);
    } else {//here you can return any type of object doing your respective control of field.
        JsonResponse msj = new JsonResponse("Error", "List empty");
        return new ResponseEntity<>(msj, HttpStatus.NOT_FOUND);
    }
}
@RequestMapping(value=“/all”,method=RequestMethod.GET)
公众反应{
List List=transfernciadao.getAll();
如果(!list.isEmpty()){
返回新的响应属性(列表,HttpStatus.OK);
}else{//在这里,您可以返回任何类型的对象来执行各自的字段控制。
JsonResponse msj=新的JsonResponse(“错误”,“列表为空”);
返回新的响应属性(msj、HttpStatus.NOT_FOUND);
}
}
如果您不想返回任何错误,也可以这样做。您可以返回类似于以下内容的内容:

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
返回新的响应属性(HttpStatus.NOT_FOUND);

我将给您留下一个示例,说明如何处理不同的对象以返回
ResponseEntity

@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<?> getAll() {
    List<Transferencia> list = transferenciaDAO.getAll();
    if (!list.isEmpty()) {
        return new ResponseEntity<>(list, HttpStatus.OK);
    } else {//here you can return any type of object doing your respective control of field.
        JsonResponse msj = new JsonResponse("Error", "List empty");
        return new ResponseEntity<>(msj, HttpStatus.NOT_FOUND);
    }
}
@RequestMapping(value=“/all”,method=RequestMethod.GET)
公众反应{
List List=transfernciadao.getAll();
如果(!list.isEmpty()){
返回新的响应属性(列表,HttpStatus.OK);
}else{//在这里,您可以返回任何类型的对象来执行各自的字段控制。
JsonResponse msj=新的JsonResponse(“错误”,“列表为空”);
返回新的响应属性(msj、HttpStatus.NOT_FOUND);
}
}
如果您不想返回任何错误,也可以这样做。您可以返回类似于以下内容的内容:

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
返回新的响应属性(HttpStatus.NOT_FOUND);

uhm,函数中确实需要返回对象
FbLoginResponse
?相反,您可以使用
ResponseEntity
并在响应实体内返回您的自定义消息。它不正确的原因是什么?它没有打印json@SotiriosDelimanolisWhat json?为什么会这样?Spring将java对象序列化为JSON,对吗?你一定是对的,我必须以某种方式返回一个JSON obj或使用ResponseEntity@SotiriosDelimanolisuhm,在你的函数中是否真的需要返回一个对象?相反,您可以使用
ResponseEntity
并在响应实体内返回您的自定义消息。它不正确的原因是什么?它没有打印json@SotiriosDelimanolisWhat json?为什么会这样?Spring将java对象序列化为JSON,对吗?你一定是对的,我必须以某种方式返回一个JSON obj或使用ResponseEntity@sotiriosdelimanolis这样做会在响应对象中返回一个长堆栈跟踪…有没有办法省去这个?!您可以只返回
HttpsStatus
它将返回一个空白响应,这将在响应对象中返回一个长堆栈跟踪…有没有办法省去这个?!您只需返回
HttpsStatus
,它将返回一个空白响应