【问题标题】:I cannot send an HTTP request (500 internal server error)我无法发送 HTTP 请求(500 内部服务器错误)
【发布时间】:2020-02-13 08:19:40
【问题描述】:

我目前收到以下关于正在发送的 http 请求的错误。我正在尝试发送一个 JSON 数组列表以在接收端触发一个方法,以便将列表保存在其数据库中。

500 Internal Server Error 是一个非常笼统的 HTTP 状态代码,表示网站服务器出现问题,但服务器无法更具体地说明问题所在。

网站以多种方式表达 500 错误,但它们基本上都在说同一件事:目前存在一般服务器问题。 大多数情况下,您无能为力,只能直接联系网站,然后等待他们修复它。 万一出现问题,请尝试清除缓存并从出现错误的站点中删除所有 cookie。

请找出以下错误:


org.springframework.web.client.HttpServerErrorException: 500 Internal Server 
    public static String FRONT_URL;

        public static String BACK_URL;

        public static final String REST_SYNC = "rest/sync";
        public static final String REST_API = "rest/api";


        private Logger log = Logger.getLogger(FrontSynchronizer.class);
        static final Logger synclog = Logger.getLogger("sync");

        ResourceBundle rb = ResourceBundle.getBundle("bundles.sync-application-resources", Locale.getDefault());
    //method sending the request
        public void syncApplications(List<SchemeApplication> accList) {

            schemeApplicationDto=new SchemeApplicationDto();

            FRONT_URL = rb.getString("sync.front.url").concat(REST_SYNC);
            BACK_URL = rb.getString("sync.back.url").concat(REST_API);

            JSONArray array = new JSONArray();
            if (accList != null && accList.size() > 0) {

                for (SchemeApplication student : accList) {

                    schemeApplicationDto.setId(student.getId());
                    schemeApplicationDto.setAccountID(student.getAccountID());
                    schemeApplicationDto.setNoOfPersonsEmployedLocal(student.getNoOfPersonsEmployedLocal());
                    schemeApplicationDto.setLocalmainclients(student.getLocalmainclients());

                    JSONObject studentJSON = new JSONObject(schemeApplicationDto);
                    array.put(studentJSON);
                }
            }
            HttpHeaders headers = new HttpHeaders();
            JSONObject object = new JSONObject();
            object.put("array", array);
            headers.setContentType(MediaType.APPLICATION_JSON);
            RestTemplate restTemplate = this.createnewTemplate();
            String url = BACK_URL.concat("/application");
            HttpEntity<String> requestEntity = new HttpEntity<String>(object.toString(), headers);
            ResponseEntity<Boolean> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity,
                    Boolean.class);

            if (responseEntity.getBody())

            {

                for(SchemeApplication scheme:accList) {

                    schemeApplicationService.getDao().delete(scheme);

                }

            }

        }

        public RestTemplate createnewTemplate() {
            // RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());

            HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
            httpRequestFactory.setConnectTimeout(120000);

            RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
            return restTemplate;
        }



// method that needs to process the request
//The method is trying to send an Array list so as the receiving end can receive the list and save it in its database.



    @RequestMapping(value = "application", method = RequestMethod.POST)
    public Boolean getAllArchivedpplications(@RequestBody String schemeJson) {
        List<SchemeApplication> accList = null;
        try {
            accList = new ArrayList<SchemeApplication>();
            if (StringUtils.isNotEmpty(schemeJson)) {

                JSONObject listObject = new JSONObject(schemeJson);
                JSONArray entryArray = listObject.getJSONArray("array");

                for (int i = 0; i < entryArray.length(); i++) {
                    JSONObject res = new JSONObject(entryArray.get(i).toString());
                    ObjectMapper mapper = new ObjectMapper();
                    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    schemeApplication doc = mapper.readValue(res.toString(),
                            new TypeReference<schemeApplication>() {
                            });
                    accList.add(doc);
                }
                schemeService.getDao().save(accList); // Service.save accountlist;

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

【问题讨论】:

    标签: java spring http post


    【解决方案1】:

    @RequestBody 必须作用于一个对象。

    【讨论】:

    • 欢迎来到 SO!请详细说明您的答案,以目前的形式,它将立即被否决或删除。如需更多指导,请参阅stackoverflow.com/help/answering
    • 对象已转换为请求实体的字符串。
    【解决方案2】:

    以两种方式完成此类工作的标准方法:

    1. 形成一个类,其类文件与您发送的 json 数据具有相同的名称和结构,并通过 @RequestBody 注释捕获该数据
    2. 当您将数据作为字符串发送时,将其作为请求参数发送,并使用@RequestParam 而不是@RequestBody 并解析您需要做的事情的方式。因为我认为对于您正在使用的这种批量数据的数组列表,选项 1 会更好/可行。 详情可以在这里查看:@RequestBody and @ResponseBody annotations in Spring

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 2014-03-25
      • 2019-11-26
      • 2013-09-13
      • 2019-04-27
      • 2022-11-01
      • 2015-09-20
      相关资源
      最近更新 更多