RestTemplate android给503服务不可用

时间:2016-11-08 18:14:49

标签: android spring rest

我尝试将手机连接到我的本地服务器(用弹簧制作)。

我在android上有这个代码:

protected Student doInBackground(Void... params) {
                try {
                    final String url = "http://localhost:8080/getUser/Marian";
                    RestTemplate restTemplate = new RestTemplate();
                    restTemplate.getMessageConverters().add(newMappingJackson2HttpMessageConverter());
                    Student hello = restTemplate.getForObject(url, Student.class);
                    return hello;
                } catch (Exception e) {
                    Log.e("MainActivity", e.getMessage(), e);
                }

                return null;
            }

在restTemplate.getForObject(...)我收到503服务不可用。

这是堆栈跟踪:

I/System.out: [OkHttp] sendRequest>>
I/System.out: [OkHttp] sendRequest<<
W/RestTemplate: GET request for "http://localhost:8080/getUser/Marian" resulted in 503 (Service Unavailable); invoking error handler
E/MainActivity: 503 Service Unavailable
                org.springframework.web.client.HttpServerErrorException: 503 Service Unavailable
                    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
                    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524)
                    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
                    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
                    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
                    at app.washtime.com.testspring.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:34)
                    at app.washtime.com.testspring.MainActivity$HttpRequestTask.doInBackground(MainActivity.java:26)

知道出了什么问题吗?

控制器:

@Controller
public class StudentController {

    @Autowired
    StudentRepository studentRepo;

    @ResponseBody
    @RequestMapping(value = "/getUser/{firstName}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public Student getUser(@PathVariable String firstName) {
        Student student = studentRepo.findByFirstName(firstName).get();
        System.out.println(student.getLastName());
        return student;
    }

0 个答案:

没有答案