在 JAX-RS 服务中获取 XML

发布于 2024-09-24 03:44:16 字数 411 浏览 3 评论 0原文

如何在 JAX-RS 服务中获取 XML 和/或 URL(字符串)?

例如在 GET 方法 URL

@GET
@Produces("application/xml; charset=UTF-8")
public JaxrsPriceWrapper getPrice(@QueryParam("firstId"), @QueryParam("materialId"),...) {
    //here I would like to get whole URL
}

和 POST 方法 XML 中

@POST
public JaxrsOrderWrapper insertOrder(OrderJaxrsVO jaxrsVO) {
    //here the XML
}

How can I get XML and/or URL (String) in JAX-RS service?

For example in GET method URL

@GET
@Produces("application/xml; charset=UTF-8")
public JaxrsPriceWrapper getPrice(@QueryParam("firstId"), @QueryParam("materialId"),...) {
    //here I would like to get whole URL
}

and in POST method XML

@POST
public JaxrsOrderWrapper insertOrder(OrderJaxrsVO jaxrsVO) {
    //here the XML
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-10-01 03:44:16

这对我使用泽西岛有用。添加一个变量;

@Context private UriInfo uriInfo;

.. 到您的资源类。这将可供资源方法使用。然后您可以调用

uriInfo.getRequestURI()

例子;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("/jerseytest")
public class Server
{
    @Context private UriInfo uriInfo;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String get()
    {
        System.out.println("jerseytest called: URI = " + uriInfo.getRequestUri());

        return "<response>hello world</response>";
    }
}

编辑:
您可能需要使用 @Consumes(MediaType.APPLICATION_XML) 注释 POST 方法才能发布数据。

This works for me using Jersey. Add a variable;

@Context private UriInfo uriInfo;

.. to your resource class. This will be made available to the resource methods. You can then call

uriInfo.getRequestURI().

Example;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

@Path("/jerseytest")
public class Server
{
    @Context private UriInfo uriInfo;

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String get()
    {
        System.out.println("jerseytest called: URI = " + uriInfo.getRequestUri());

        return "<response>hello world</response>";
    }
}

Edit:
You probably need to annotate your POST method with @Consumes(MediaType.APPLICATION_XML) to get the data posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文