从 Java 中的 RESTful Web 服务方法返回整数 [英] Returning an Integer from RESTful web services method in java

查看:29
本文介绍了从 Java 中的 RESTful Web 服务方法返回整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RESTful Web 服务的新手.我编写了一个 Web 服务方法,目前正在使用 @Produces(MediaType.TEXT_XML) 返回一个 XML.但要求是我的 Web 服务方法必须返回一个整数而不是 XML.我怎样才能做到这一点?请帮我解决这个问题.下面是我写的方法.

@Path("{type}/{token}")@得到@Produces(MediaType.TEXT_XML)public String setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("type") String type, @PathParam("token") String token) throws ServletException, IOException {字符串值=令牌;如果(请求==空){System.out.println("请求为空");}System.out.println("令牌:" + value);System.out.println("AnumLotnum:" + type);如果(请求!=空){request.setAttribute("param", value);request.getRequestDispatcher("/dummy").include(request, response);}String id=request.getAttribute("ID").toString();return ""+ ""+id+""+ "</令牌>";}

解决方案

使用 @Produces(MediaType.TEXT_PLAIN) 而不是 @Produces(MediaType.TEXT_XML)>

当您使用纯 HTTP 实现(超文本传输​​协议)的 RESTful Web 服务时,它将是文本.

要获得一个整数,您可以手动在客户端进行转换.

实际上,您有两个选项可以返回 Stringint,两者都可以.通过返回 int,像 Jersey Http 客户端这样的智能客户端会自动将 String 转换为 int.

示例

 @Path("{type}/{token}")@得到@Produces(MediaType.TEXT_PLAIN)public int setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("type") String type, @PathParam("token") String token) throws ServletException, IOException {返回 1;}

Am new to RESTful web services. I wrote a web service method and am currently returning an XML by using @Produces(MediaType.TEXT_XML). But the requirement is that my web service method must return an Integer instead of XML. How can I achieve this? kindly help me with this. Below is the method I wrote.

@Path("{type}/{token}")
@GET
@Produces(MediaType.TEXT_XML)
public String setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("type") String type, @PathParam("token") String token) throws ServletException, IOException {
    String value=token;
    if(request==null){
        System.out.println("Request null");
    }

    System.out.println("Token: " + value);
    System.out.println("AnumLotnum: " + type);

    if(request!=null){
        request.setAttribute("param", value);
        request.getRequestDispatcher("/dummy").include(request, response);
    }

    String id=request.getAttribute("ID").toString();
    return "<token>"+ "<value>"+id+"</value>" + "</token>";
}

解决方案

Use @Produces(MediaType.TEXT_PLAIN) instead of @Produces(MediaType.TEXT_XML)

It will be text as you are using RESTful Webservices which is a pure HTTP implementation(Hyper TEXT Transfer Protocol).

To get an integer you can either cast at Client side manually.

Actually you have two options you may either return String or int, both will do. By returning int, a smart Client like Jersey Http client will automatically cast the String to int for you.

Example

  @Path("{type}/{token}")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public int setToken(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("type") String type, @PathParam("token") String token) throws ServletException, IOException {
return 1;     
}

这篇关于从 Java 中的 RESTful Web 服务方法返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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