Returning an Integer from RESTful web services method in java

后端 未结 1 1177
庸人自扰
庸人自扰 2021-01-24 21:53

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 m

1条回答
  •  隐瞒了意图╮
    2021-01-24 22:09

    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;     
    }
    

    0 讨论(0)
提交回复
热议问题