开发者

How I should write Jersey rest service to make is accessible by given spring code

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 unch开发者_StackOverflow社区anged spring code.


When you type:

@Consumes("application/x-www-form-urlencoded")

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜