我试图通过 resttemplate 命中 restful api 但我收到请求处理失败
I am trying to hit restful api through resttemplate but i am getting Request processing failed
错误:
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 +
'}';
}
}
控制器class:
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";
}
}
引用 class:
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 +
'}';
}
}
您的 import org.springframework.beans.factory.annotation.Value
在 Quote.class
Json 对象中可能有误。
来自 RequestingURL 的回复
你正在尝试按如下方式命中。
您必须根据服务器返回的响应设计您的 Json 对象。
{
"type": "success",
"value": {
"id": 4,
"quote": "Previous to Spring Boot, I remember XML hell, confusing set up, and many hours of frustration."
}
}
您可能需要创建另一个具有属性 id
和 quote
的 POJO 对象 Value
,并将其定义为 Quote
中的 属性 class。
错误:
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 +
'}';
}
}
控制器class:
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";
}
}
引用 class:
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 +
'}';
}
}
您的 import org.springframework.beans.factory.annotation.Value
在 Quote.class
Json 对象中可能有误。
来自 RequestingURL 的回复 你正在尝试按如下方式命中。
您必须根据服务器返回的响应设计您的 Json 对象。
{
"type": "success",
"value": {
"id": 4,
"quote": "Previous to Spring Boot, I remember XML hell, confusing set up, and many hours of frustration."
}
}
您可能需要创建另一个具有属性 id
和 quote
的 POJO 对象 Value
,并将其定义为 Quote
中的 属性 class。