我应该如何编写 Jersey Rest 服务以通过给定的 Spring 代码进行访问
这是使用 Spring 编写的休息客户端:
public void addGadget(String gadgetName, String gadgetUrl) {
Map<String, String> map = new HashMap<String, String>();
map.put("gadgetName", gadgetName);
map.put("gadgetUrl", gadgetUrl);
restTemplate.postForLocation(restServiceUrl, map);
}
这不是我的代码,我无法更改它。我应该写休息服务,但没有弹簧,使用球衣。这是我的代码,但它不起作用:
@Path("gadgets")
public class RestService {
@POST
@Consumes("application/x-www-form-urlencoded")
public Response addGadget(@FormParam("gadgetUrl") String gadgetUrl,
@FormParam("gadgetName") String gadgetName) throws Exception {
//some logic
return Response.status(201).build();
}
}
当我尝试使用 spring 客户端访问此服务时,出现异常:
org.springframework.web.client.HttpClientErrorException:415 不支持的媒体类型
我应该如何重写我的服务声明(我猜问题出在 Consumes 注释上),以便 Spring 客户端可以使用未更改的 Spring 代码来访问它。
Here is rest client that is written using Spring:
public void addGadget(String gadgetName, String gadgetUrl) {
Map<String, String> map = new HashMap<String, String>();
map.put("gadgetName", gadgetName);
map.put("gadgetUrl", gadgetUrl);
restTemplate.postForLocation(restServiceUrl, map);
}
This is not my code and I cannot change it. I should write rest service, but without spring, using jersey. Here is my code but it doesn't work:
@Path("gadgets")
public class RestService {
@POST
@Consumes("application/x-www-form-urlencoded")
public Response addGadget(@FormParam("gadgetUrl") String gadgetUrl,
@FormParam("gadgetName") String gadgetName) throws Exception {
//some logic
return Response.status(201).build();
}
}
When I try to access this service using spring client, I get exception:
org.springframework.web.client.HttpClientErrorException: 415
Unsupported Media Type
How should I re-write my service declaration (I guess the problem is with Consumes annotation) in order to make it accessible for spring client with unchanged spring code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您键入时:
您正在缩小您的服务可接受的类型。请尽量不要这样做,只需删除这行代码,看看会发生什么。如果它没有帮助,则意味着 RestTemplate 设置为使用某种未知的媒体类型。如果是,请检查 Spring RestTemplate 对象发送的媒体类型是什么。如果删除 @Consumes 无助于写入从 RestTemplate 发送的媒体类型,以便我可以提供更多帮助。
When you type:
You are narrowing the type acceptable by your service. Please try not to do this and just erase this line of code and see what happens. If it will not help it means that RestTemplate is set up to use some unknown media type. If so please check what is the media type sent by Spring RestTemplate object. If removing @Consumes will not help write what is the media type sent from RestTemplate so that I could help more.