-1

I have following code

@Path("/hello")
public class Hello {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_XML)
    public String sayPlainTextHello(@Context UriInfo wsContext) 
    {
        return "hello from xml";
    }
}

This will result in a path http://localhost:8080/WSTest/rest/hello. Now i want to have 2 different method so that when a user calls http://localhost:8080/WSTest/rest/hello.xml it goes to XML method and when it calls http://localhost:8080/WSTest/rest/hello.json it goes to JSON method. With current implementation, if i add .xml or .json at the end of the path, the server returns not found error.

EDIT here is my web.xml. Please note that if add a subpath lets say @Path("{name}.xml") which means the web.xml is fine.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WSTest</display-name>
  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.personal.ws.test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

P.S: I can't change the path, we want to keep the root path as http://localhost:8080/WSTest/rest/hello it is.

4

2 回答 2

0

您需要在@Path方法中添加注释:

@Path("/rest")
public class Hello {

    @Path("/hello.xml")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.TEXT_XML)
    public String sayXmlHello(@Context UriInfo wsContext) {
        return "hello from xml";
    }

    @Path("/hello.json")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_JSON)
    public String sayJsonHello(@Context UriInfo wsContext) {
        return "hello from json";
    }
}
于 2012-07-28T23:21:19.190 回答
0

您可以按照@Ali 的建议通过让客户端请求正确的编码来执行此操作。

您可以通过添加扩展支持来增强此功能。这样,无法设置他们想要的响应格式(无论出于何种原因)的 Web 浏览器和客户端仍然可以切换媒体类型。你可以通过扩展来做到这一点PackagesResourceConfig

public class CommonResourceConfig extends PackagesResourceConfig {

  public CommonResourceConfig(Map<String, Object> props) {
    super(props);
  }

  public CommonResourceConfig(String... packages) {
    super(packages);
  }

  @Override
  public Map<String, MediaType> getMediaTypeMappings() {
    Map<String, MediaType> map = new HashMap<String, MediaType>();
    map.put("xml", MediaType.APPLICATION_XML_TYPE);
    map.put("json", MediaType.APPLICATION_JSON_TYPE);
    return map;
  }
}

然后告诉 Jersey 通过将javax.ws.rs.Application属性设置为该类的完全限定名称来查找此配置(在您的 web.xml 中,除非您使用某些 DI 机制)。此外,您还需要更新方法上的 @Produces 注释以支持您要处理的所有媒体类型。这样,您不必为了更改服务的媒体类型而在方法之间复制功能。

编辑: Hello.java 与您所拥有的完全一样,CommonResourceConfig 如上所述(全部在 中src/main/java/com/personal/ws/test/)。这是我的其余配置(至少在本地):

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>jersey</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>jerseyTest</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.0.3.v20111011</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.13</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>1.13</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-guice</artifactId>
            <version>1.13</version>
        </dependency>
    </dependencies>
</project>

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <display-name>WSTest</display-name>

    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.personal.ws.test</param-value>
        </init-param>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.personal.ws.test.CommonResourceConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

运行 amvn jetty:run然后 wget 或使用浏览器 for http://localhost:8080/rest/hello.jsonand http://localhost:8080/rest/hello.xml。我得到了正确的结果......也许只是一些小的配置问题?

于 2012-07-29T18:53:12.137 回答