【发布时间】:2019-07-25 17:19:23
【问题描述】:
我正在将一个应用程序/json 类型从邮递员客户端发送到一个 java API,该 API 将所有请求转发到该案例的特定 API。 在这个具体案例中,我有一个登录 API,我想集中代码听到这个 JSON:
来自邮递员的 JSON
{
"name": "random name",
"password": "random passwd"
}
执行转发的 API
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String redirectHttpRequest(HttpServletRequest request, @Value("${endpoint}") String newURL,
HttpServletResponse response) throws IOException {
try {
RestTemplate restTemplate = new RestTemplate();
String result = null;
String body = IOUtils.toString(request.getReader());
if (request.getMethod().equals("GET")) {
// result = restTemplate.getForObject(redirectUrl.toString(), String.class);
} else if (request.getMethod().equals("POST")) {
result = restTemplate.postForObject(newURL, body, String.class);
}
return result;
} catch (HttpClientErrorException e) {
System.out.println(e);
return "OLA";
}
}
新的 URL 是另一个 API 的 URL(在这种情况下,它是 localhost:8080 并且来自 application.properties 文件)。
我已经通过邮递员测试了登录 API,它可以工作,但是当我尝试将其连接到该转发 API 时,我收到以下错误:
org.springframework.web.client.HttpClientErrorException: 415 null。
我想知道我做错了什么或另一种方法。
用户类
公共类用户{
private String name;
private String password;
private List<String> groups;
public User(String name, String password) {
this.name = name;
this.password = password;
this.groups = new ArrayList<String>();
}
public User() {
}
public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
public List<String> getGroups() {
return this.groups;
}
public String toString() {
return "User: " + this.name + "\nGroups: " + this.groups;
}
【问题讨论】:
-
为了澄清的目的,两个端点都在同一台服务器上吗?
localhost:8080在你的情况下? -
不,它们在不同的服务器上。一个在 8080 上,另一个在 8082 上
-
所以
localhost:8082上的第一个和localhost:8080上的第二个?相同的服务器,但不同的端口 -
转发的是localhost:8082/forward,登录的是localhost:8080/login。我可以在他们两个之间进行获取请求,但不能发帖,我不知道为什么。
-
您能否将
postForObject调用时body和newURL中的值添加到您的帖子中,以确保它们看起来正确
标签: java rest spring-boot post