SpringFrameWork:500内部服务器错误

时间:2015-09-04 00:00:14

标签: android json spring rest parsing

我正在使用Android应用程序,通过REST服务从服务器获取数据。

现在我必须使用Body在REST服务上进行POST。但我这样做有问题。我正在使用SPRINGFRAMEWORK与REST服务进行通信。但我有这个错误:

org.springframework.web.client.HttpServerErrorException:500内部服务器错误

以下是我发布的代码:

org.springframework.web.client.HttpServerErrorException:500内部服务器错误

public void shiftRight() 
{
    //make temp variable to hold last element
    int temp = listValues.get(listValues.size()-1); 

    //make a loop to run through the array list
    for(int i = listValues.size()-1; i > 0; i--)
    {
        //set the last element to the value of the 2nd to last element
        listValues.set(i,listValues.get(i-1)); 
    }
    //set the first element to be the last element
    listValues.set(0, temp);     
}

对于发布我使用了两种方式,但没有一种方法有效:

final String url = "http://mywebsite.com/login";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);



HttpEntity<Credentials> entity = new HttpEntity<>(credentials ,requestHeaders);

//the object "CREDENTIALS" has the valus that should be sent as BODY in the REST Service

ResponseEntity<AllData> response = restTemplate.exchange(url, HttpMethod.POST, entity, AllData.class);

知道我遇到这个问题的原因吗?

P.S。我检查了许多像我一样的问题,但没有一个我能找到答案。我好几天都在努力,但无法弄清问题是什么:@

2 个答案:

答案 0 :(得分:1)

我是完全相同的问题,我设法解决了它。举个例子,我需要将Credentials实体类型更改为相同的ResponseEntity类型。会是这样的:

final String url = "http://mywebsite.com/login";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);


AllData allData = new AllData();

HttpEntity<AllData> entity = new HttpEntity<>(allData, requestHeaders);

ResponseEntity<AllData> response = restTemplate.exchange(url, HttpMethod.POST, entity, AllData.class);

答案 1 :(得分:0)

既然你说它是从邮递员和其他人那里工作的,我猜它是编码或标题问题。 我想找到问题的最简单方法是使用像fiddler这样的代理,并检查将Android设备发送的标头和数据与邮递员或其他工具发送的标题和数据进行比较