Spring MVC - 从另一个休息服务中调用休息服务 [英] Spring MVC - Calling a rest service from inside another rest service

查看:149
本文介绍了Spring MVC - 从另一个休息服务中调用休息服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个非常奇怪的问题,从另一个内部调用一个REST服务,我真的可以帮助解决我做错了什么。

I'm currently having a really weird issue with calling one REST service from inside another one and I could really use a hand in working out what I'm doing wrong.

首先,有点上下文:

我有一个调用REST服务来创建用户帐户的webapp (为了便于解释,端点是localhost:8080 / register)。在用户旅程的早期,我调用了一个不同的服务来创建用户的登录凭据 localhost:8090 / signup 但是我需要在调用/注册时查看一些内容所以在通话中我正在呼叫8090上的另一个端点以获取此信息( localhost:8090 / availability )。简而言之,webapp调用localhost:8080 / register,然后调用 localhost:8090 / availability

I have a webapp which calls off to a REST service to create a user account (for the sake of this explanation, the endpoint is localhost:8080/register). Earlier in the user journey I've called a different service to create the user's login credentials localhost:8090/signup but I need to check a few things in the call to /register so inside the call I'm calling out to a different endpoint on 8090 to get this information (localhost:8090/availability). Long story short, the webapp calls localhost:8080/register which in turn calls localhost:8090/availability.

当我直接从REST客户端或webapp本身调用可用性端点时,一切都按预期工作,但由于某些奇怪的原因,当我从调用寄存器端点调用它时,我得到一个HTTP415。任何人都对出现问题有什么了解吗?

When I call the availability endpoint directly, from either a REST client or the webapp itself, everything works as expected, but for some strange reason, when I call it from inside the call to the register endpoint I get a HTTP415. Anyone have any insight into what's going wrong?

寄存器控制器如下所示:

The register controller looks like this:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public UserModel createUser(@RequestBody UserModel userModel) throws InvalidSignupException {

    // a load of business logic that validates the user model

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<Boolean> response = restTemplate.postForEntity("http://localhost:8090/availability",
            userModel.getUsername(), Boolean.class);
    System.out.println(response.getBody());

    // a load more business logic

    return userModel;
}

可用性控制器如下所示:

And the availability controller looks like this:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Boolean isUsernameAvailable(@RequestBody String username) {

    // a load of business logic that returns a boolean
    return Boolean.TRUE;
}

完全披露 - 在实践中,我所显示的是createUser的内容()实际上是几次调用调用堆栈,使用与我用来从webapp调用服务相同的类(在该上下文中工作得很好),而我实际上并不只是在isUsernameAvailable中返回true(因为那会是愚蠢的)但这是复制问题的最简单的代码版本。

Full disclosure - in practice, what I've shown as the contents of createUser() are actually several calls up the call stack, using the same class as I use to call the services from the webapp (which works perfectly well in that context), and I'm not actually just returning true in isUsernameAvailable (because that would be silly) but this is the simplest version of the code that replicates the issue.

我目前的假设是,我正在做的事情,当我看到它时,我会把自己踢开,但我一直盯着这段代码能够再看到它了。

My current assumption is that I'm doing something that I'm going to kick myself over when I see it but I've been staring at this code too long to be able to see it any more.

编辑下面的Vikdor评论为我解决了这个问题。我将createUser方法更改为:

Edit Vikdor's comment below solved this problem for me. I changed the createUser method to:

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public UserModel createUser(@RequestBody UserModel userModel) throws InvalidSignupException {

    // a load of business logic that validates the user model

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(Arrays.asList(new MappingJackson2HttpMessageConverter()));
    ResponseEntity<Boolean> response = restTemplate.postForEntity("http://localhost:8090/availability",
            userModel.getUsername(), Boolean.class);
    System.out.println(response.getBody());

    // a load more business logic

    return userModel;
}


推荐答案

HTTP415 表示不支持的媒体类型。这意味着 isUsernameAvailable 期望以JSON格式输入,但这不是它得到的。

A HTTP415 means Unsupported Media Type. What that means is that isUsernameAvailable expects input in JSON format, but that isn't what it is getting.

尝试通过执行以下操作,向您的HTTP请求显式添加 Content-Type:application / json 标头:

Try explicitly adding Content-Type: application/json header to your HTTP request by doing the following:

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

HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
restTemplate.put(uRL, entity);

这篇关于Spring MVC - 从另一个休息服务中调用休息服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆