Announcement Announcement Module
Collapse
No announcement yet.
Get web service message when I get 422 error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get web service message when I get 422 error

    I'm developing an Android application that connects to a Web service. From the web service I get this error:

    Code:
    POST request for "xxx" resulted in 422 (Unprocessable Entity); invoking error handler
    I'm using SpringFramework Rest Client for Android, and I connect to the web service with this code:

    Code:
    	public static User sendUserPersonalData(User userProfileData)
    	{
    		try
    		{
    			HttpHeaders requestHeaders = new HttpHeaders();
    			requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
    			HttpEntity<User> requestEntity = new HttpEntity<User>(userProfileData, requestHeaders);
    
    			GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
    			List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    			messageConverters.add(messageConverter);
    
    			HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    			requestFactory.setReadTimeout(readTimeout);
    
    	        RestTemplate restTemplate = new RestTemplate(requestFactory);
    	        restTemplate.setMessageConverters(messageConverters);
    
    	        String url = URL_BASE_WEB + USER_PERSONAL_DATA_CALL;
    	        ResponseEntity<User> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, User.class);
    
    	        return responseEntity.getBody();
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    		return null;
    	}

    But, the web service also returns a JSON string like this one:

    Code:
        {
            "email": [
                "is invalid"
            ],
            "birthday": [
                "is invalid"
            ],
            "startday": [
                "is invalid"
            ],
            "sex_preference": [
                "can't be blank"
            ],
            "password": [
                "is too long (maximum is 4 characters)"
            ]
        }
    How can I get the response even if I get an Exception?

  • #2
    You catch the appropriate exception and use getResponseBodyAsString(), then you can use Gson/Jackson/whatever to parse it once you know it is JSON.

    For example:
    Code:
    		} catch (HttpClientErrorException e) {
    			success = false;
    			Log.e(TAG, command+" was rejected for URL: "+url, e);
    			resultCode = e.getStatusCode().value();
    			response = e.getResponseBodyAsString();
    		} catch (HttpServerErrorException e) {
    			success = false;
    			Log.e(TAG, command+" could not be completed for URL: "+url, e);
    			resultCode = e.getStatusCode().value();
    			response = e.getResponseBodyAsString();
    		} catch (ResourceAccessException e) {
    			if (attemptsLeft > 0) {
    				Log.w(TAG, command+" failed I/O, retrying.", e);
    				return simplePut(baseUrl, url, b, command, payload, attemptsLeft);
    			}
    			success = false;
    			Log.e(TAG, command+" failed I/O for URL: "+url, e);
    			resultCode = 499;
    		} catch (RestClientException e) {
    			success = false;
    			Log.e(TAG, command+" failed for URL: "+url, e);
    			resultCode = 599;
    		}

    Comment

    Working...
    X