无法推断ResponseEntity <>的类型参数

时间:2019-06-26 20:03:41

标签: java spring spring-boot spring-mvc

我想实现Spring端点,在其中可以返回XML对象NotificationEchoResponse和http状态代码。我尝试过:

@PostMapping(value = "/v1/notification", produces = "application/xml")
  public ResponseEntity<?> handleNotifications(@RequestParam MultiValueMap<String, Object> keyValuePairs) {

   if (!tnx_sirnature.equals(signature)) 
     {
         return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR);
     }         

    return new ResponseEntity<>(new NotificationEchoResponse(unique_id), HttpStatus.OK);
  }

但是我在以下行得到错误:Cannot infer type arguments for ResponseEntity<>return new ResponseEntity<>("Please contact technical support!", HttpStatus.INTERNAL_SERVER_ERROR);您知道如何解决此问题吗?

2 个答案:

答案 0 :(得分:1)

您可以使用

  public class ResponseData {
      private Object payload;
   } 

喜欢

OR

您可以使自己拥有自定义类,如ResponseData,并在该类中放入诸如paylod的字段

    @PostMapping(value = "/v1/notification", produces = "application/xml")
    public ResponseEntity<ResponseData> handleNotifications(@RequestParam 
    MultiValueMap<String, Object> keyValuePairs) {

   if (!tnx_sirnature.equals(signature)) 
   {
     return new ResponseEntity<ResponseData>(new ResponseData("Please contact to technical support"), 
    HttpStatus.INTERNAL_SERVER_ERROR);
   }         

   return new ResponseEntity<ResponseData>(new ResponseData(new NotificationEchoResponse(unique_id)), 
   HttpStatus.OK);
   }

并使用该ResponseEntity并设置该值。

现在您的控制器将像这样

  @PostMapping(value = "/v1/notification", produces = "application/xml")
    public ResponseEntity<Object> handleNotifications(@RequestParam 
    MultiValueMap<String, Object> keyValuePairs) {

   if (!tnx_sirnature.equals(signature)) 
   {
     return new ResponseEntity<Object>("Please contact to technical support", 
    HttpStatus.INTERNAL_SERVER_ERROR);
   }         

   return new ResponseEntity<Object>(new NotificationEchoResponse(unique_id), 
   HttpStatus.OK);
   }

您还可以使用Object替换响应数据,然后使用

class A:
    def read_json(self,file_json_path):
        try:
            with open(file_json_path) as fd:
                content = json.dumps(fd)
         except IOError:
            print 'exception while opening a file %s\n'%(file_json_path)

答案 1 :(得分:0)

public ResponseEntity<BasicCreateUpdateResponse> deleteProductPrefix(@RequestBody ProductPrefixEntity inputFields) {
    if(inputFields.getRecid().isEmpty()) {
        throw new ApplicationException(NPIConstants.ERR_500, HttpStatus.OK, null, null);
    }
    logger.info("Inside resources updateProduct method() .........");
    return new ResponseEntity<>(productPrefixService.deleteProductPrefix(inputFields),HttpStatus.OK);
}

在此代码中,返回的“ productPrefixService.deleteProductPrefix(inputFields)”类型必须为ResponseEntity。