我试图通过resttemplate命中restful api,但我收到请求处理失败

时间:2015-10-15 10:31:54

标签: resttemplate

错误:

package com.concretepage.bean;

import org.springframework.beans.factory.annotation.Value;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }
}

控制器类:

package com.concretepage.controller;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import com.concretepage.bean.Quote;

@Controller
public class LoginController {


    @RequestMapping(value="login", method = RequestMethod.GET)
    public String login(){
        System.setProperty("proxyHost", "proxy1.wipro.com"); 
        System.setProperty("proxyPort", "8080");
        return "redirect:pages/login.jsp";
    }

    @RequestMapping(value="pages/userCheck", method = RequestMethod.POST)
    public String userCheck(ModelMap model, HttpServletRequest request) {
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        if("concretepage".equalsIgnoreCase(name)&&"concretepage".equalsIgnoreCase(pwd)){

            model.addAttribute("message", "Successfully logged in.");

             RestTemplate restTemplate = new RestTemplate();

             List<HttpMessageConverter<?>> messageConverters =restTemplate.getMessageConverters();
             MappingJacksonHttpMessageConverter map =new MappingJacksonHttpMessageConverter();
              messageConverters.add(map);
              restTemplate.setMessageConverters(messageConverters);

                Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);

                System.out.println(quote.toString());


        }else{
            model.addAttribute("message", "Username or password is wrong.");
        }
        return "redirect:success.jsp";
    }

}

Qoute课程:

package com.concretepage.bean;

import org.springframework.beans.factory.annotation.Value;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

    private String type;
    private Value value;

    public Quote() {
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Value getValue() {
        return value;
    }

    public void setValue(Value value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Quote{" +
                "type='" + type + '\'' +
                ", value=" + value +
                '}';
    }
}

1 个答案:

答案 0 :(得分:0)

import org.springframework.beans.factory.annotation.Value Json对象中Quote.class可能出错。

来自RequestingURL的回复  你试图击中如下。

您必须根据服务器返回的响应设计Json对象。

{
  "type": "success",
  "value": {
   "id": 4,
    "quote": "Previous to Spring Boot, I remember XML hell, confusing set  up,   and many hours of frustration."
 }
}

您可能必须使用属性Valueid创建另一个POJO对象quote,并将其定义为Quote类中的属性。