我无法发送http请求(500内部服务器错误)

k4ymrczo  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(385)

我目前收到的http请求发送以下错误。我试图发送一个json数组列表来触发接收端的一个方法,以便它将列表保存在数据库中。
500内部服务器错误是一个非常通用的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;
        }
    }
s3fp2yjn

s3fp2yjn1#

@requestbody必须处理对象。

x33g5p2x

x33g5p2x2#

这类工作的标准方法有两种:
形成一个类,其中的类文件与您发送的json数据具有相同的名称和结构,并通过 @RequestBody 注解
当您以字符串形式发送数据时,请将其作为请求参数发送,并使用 @RequestParam 而不是 @RequestBody 分析你做事的方式。因为我认为对于您正在处理的这种批量数据的arraylist,选项1会更好/可行。有关详细信息,您可以在这里查看:@requestbody和@responsebody annotations in spring

相关问题