Announcement Announcement Module
Collapse
No announcement yet.
Problem using RestTemplate's put method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using RestTemplate's put method

    I have really been enjoying spring social. I am extending to to fit my needs in accessing a restful api.

    Trouble arises when attempting to use Rest Template's put/post method though .

    GET works
    Code:
    UserProfile b = getRestTemplate().getForObject(userBaseUrl+id+ "?format=json", UserProfile.class);
    PUT crashes with 500 Internal Server Error
    Code:
    getRestTemplate().put( userBaseUrl+id, user, UserProfile.class);
    UserProfile is using Mixins in the same fashion as the netflix extension sample to serialize and deserialize json/pojos.

    Any ideas as to why I am unable to use PUT/POST? Thanks!

  • #2
    The 500-level error indicates a server-side error (assuming that the REST API is reporting errors with the proper HTTP status codes). There's no way to know from the information you've given as to why you'd get a 500 without knowing more about the REST API and what endpoint URL you're posting to. One possibility that you are hinting to is that the UserProfile may be serializing into something that the server doesn't like. Of course, even if that's the case, I can't tell what it's complaining about without knowing what values are in the user object and how it's being serialized into JSON.

    In short, there are a lot of unknowns here.

    I'm guessing that the REST API might be returning additional error data in the response body. Unfortunately, RestTemplate's put() method returns void, so any of that is lost. If you can do a postForObject() to the same URL and retrieve a String as the return object, then you can look at the results and see if there are any clues. You could also do the PUT request via a call to RestTemplate's exchange() method to get the response body back.

    Comment


    • #3
      I can post for community which has the same problem as userprofile.

      Nothing is returned to s since it crashes the program.
      Code:
      s = getRestTemplate().postForObject(communityURL, newCommunity, String.class);
      I believe it is fine to deserialize / serialize in the same mixin association? Here is what that looks like.
      Code:
      @JsonIgnoreProperties(ignoreUnknown = true)
      @JsonSerialize(using=CommunitySerializer.class)
      @JsonDeserialize(using=CommunityDeserializer.class)
      public class CommunityMixin {
      
      		static class CommunityDeserializer extends JsonDeserializer<List<Community>> {
      			@Override
      			public List<Community> deserialize(JsonParser jp, DeserializationContext context) throws IOException, JsonProcessingException {
      
      				JsonNode tree = jp.readValueAsTree();
      				List<ommunity> communities = new LinkedList<Community>();
      				
      		
      				for(JsonNode j : tree.findValues("data")){
      					communities.add(new Community(j.get("id").getValueAsLong(), j.get("name").getValueAsText(), j.get("description").getValueAsText() , j.get("shortName").getValueAsText(), j.get("userCount").getValueAsLong(), j.get("stateChangedDate").getValueAsLong(), j.get("liveDate").getValueAsLong()));
      				}
      				return communities;
      
      			}
      			}
      		static class CommunitySerializer extends JsonSerializer<Community> {
      
      			@Override
      			public void serialize(Community value, JsonGenerator jgen,
      					SerializerProvider provider) throws IOException,
      					JsonProcessingException {
      				// TODO Auto-generated method stub
      				Community q = value;
      				jgen.writeStartObject();
      				jgen.writeStringField("name", q.getName());
      				jgen.writeStringField("description", q.getDescription());
      				jgen.writeEndObject();
      				
      				jgen.close();
      				
      			}
      		
      		}
      		}
      Last edited by dinorage32; Sep 29th, 2011, 04:57 PM.

      Comment


      • #4
        Take a look at Spring Social's TwitterTemplate and how it registers a TwitterErrorHandler. If you setup an error handler with RestTemplate, you should be able to get at the response body in its handleError() method. To start, I might create a simple error handler that does nothing but log the body of the response and then calls super.handleError().

        Again, I can't say for certain why you get a 500. Is this a public API that I can see and read about...perhaps post a few things to myself? Without knowing anything about the REST API, the only thing I can say for certain is that you got an internal server error. Why the server failed on this request could be almost anything. If this is an internal service and if you have access to its logs, then maybe you can see if it's logging anything useful when it fails.

        Comment


        • #5
          thanks Craig! I set up my ErrorHandler and I am now getting useful information from the body of the response.

          Comment

          Working...
          X