5

我使用 Eclipse 创建了一个 Restful Web 服务简单项目。当我尝试编译它时,我收到以下错误。

不知道为什么不支持注释,我使用 NetBeans IDE 做了同样的事情,
它运行良好,没有任何问题。

**[INFO] Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported in -source 1.3
[INFO] ------------------------------------------------------------------------
[DEBUG] Trace
org.apache.maven.BuildFailureException: Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported in -source 1.3
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:715)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
    at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
    at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
    at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
    at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
    at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure
C:\Users\gopc\workspace\RestfulService\src\restfu\Hello.java:[18,1] error: annotations are not supported** in -source 1.3

这是我的 pom 文件结构:

POM 文件

<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>RestfulService</groupId>
  <artifactId>RestfulService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <outputDirectory>${basedir}/build/classes</outputDirectory>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
</pluginManagement>

</build>

<dependencyManagement>
<dependencies>

  <dependency>
    <groupId>jersey-server</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/jersey-server-1.4.jar</systemPath>
  </dependency>

<dependency>
        <groupId>javax-ws</groupId>
        <artifactId>javax-ws</artifactId>
        <version>1.4</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/javax.ws.rs.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
</project>

你好.java

package restfu;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// Plain old Java Object it does not extend as class or implements 
// an interface

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  //@Produces(MediaType.TEXT_XML)
  @Produces("application/xml")
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

  // This method is called if JSON is request
  @GET
  //@Produces("appplication/json")
  @Produces(MediaType.APPLICATION_JSON)
  public String sayJsonHello() {
    return "{ \"HTML\": { \"title\": \"Hello Jersey\", \"body\": { \"h1\": \"Hello Jersey\" }}" ;
  }    
} 
4

3 回答 3

8

使用以下内容增强您的 pom:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

您需要至少给出 1.5,但最好是 1.6。

我建议不要更改源代码的默认位置。最好将您的源代码移动到 maven 的默认位置:src/main/java 并从您的 pom 中删除配置:

<build>
   <sourceDirectory>${basedir}/src</sourceDirectory>
   <outputDirectory>${basedir}/target/classes</outputDirectory>
</build>
于 2013-01-03T12:14:43.073 回答
7

在(如果可能在父 pom 中)添加 java 版本1.5 或更高版本:pluginManagement

<pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>
    </plugins>
</pluginManagement>
于 2013-01-03T13:06:11.877 回答
0

谢谢我在basedir中创建了lib文件夹,然后将库文件复制到其中,然后添加了如下依赖项,然后编译问题解决了,它创建了jar文件和类文件。

<dependencies>
  <dependency>
    <groupId>jersey-server</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/jersey-server-1.4.jar</systemPath>
  </dependency>

    <dependency>
    <groupId>javax-ws</groupId>
    <artifactId>javax-ws</artifactId>
    <version>1.4</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/javax.ws.rs.jar</systemPath>
  </dependency>
于 2013-01-04T03:17:27.650 回答