0

I am currently receiving the following error for the http request am sending. I am trying to send a JSON Array list to trigger a method in the receiving end so as it saves the list in its database.

The 500 Internal Server Error is a very general HTTP status code that means something has gone wrong on the website's server, but the server could not be more specific on what the exact problem is.

Websites phrase 500 errors in many ways but they're all basically saying the same thing: there's a general server issue going on right now. Most of the time there isn't anything you can do but contact the website directly and then wait on them to fix it. In the off chance there is a problem on your end, try clearing the cache and deleting any cookies from the site with the error.

Please find the error below:


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;
        }
    }

2 Answers2

0

@RequestBody must work on an object.

spainpie
  • 1
  • 1
  • 1
    Welcome to SO! Please elaborate a bit on your answer, in the current form it will be immediately downvoted or deleted. For more guidance, see https://stackoverflow.com/help/answering – B--rian Feb 13 '20 at 09:15
  • the object was converted to a String for the request entity. –  Feb 13 '20 at 09:19
0

Standard way to do this kind of work in two ways:

  1. Form a class having class files with same name and structure with your json data you are sending and capture that data in by @RequestBody annotation
  2. As you are sending data as String, send it as request param, and use @RequestParam instead of @RequestBody and parse the way you need to do things. For I think for this kind of arrayList of bulk data you are working with, option 1 will be better/feasible. For details you can check here: @RequestBody and @ResponseBody annotations in Spring
user404
  • 1,934
  • 1
  • 16
  • 32