Java @Path and @Produces error

Asked

Viewed 212 times

1

Error in the use of @Path in Java.

package resource;

import java.util.ArrayList;
import java.util.List;
import java.nio.file.Path;
import java.nio.file.Paths;

import domain.Album;
import domain.Banda;
import domain.Musica;
import jdk.nashorn.internal.objects.annotations.Getter;

@Path ("/banda")
public class BandaService {

    @Getter
    @Path ("/get")
    @Produces("application/xml")
    public Banda getUserInXML() {

        Banda banda = new Banda();
        banda.setNome("Led Zeppelin");

        Album album = new Album();
        album.setAno(1969);
        album.setNome("Led Zeppelin");

        Musica musica1 = new Musica();
        Musica musica2 = new Musica();
        List<Musica> musicas = new ArrayList<Musica>();

        musica1.setNome("Good Times Bad Times");
        musica2.setNome("You Shook Me");
        musicas.add(musica1);
        musicas.add(musica2);

        album.setMusicas(musicas);
        banda.setAlbum(album);

        return banda; 
    }
}

Code of the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>musicApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
  </context-param>
  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
    org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

JAVA Buid Path

inserir a descrição da imagem aqui

  • Why the import jdk.nashorn.internal.objects.annotations.Getter and the annotation @Getter in the method?

  • Import made by IDE, I must change to which class ?

1 answer

2


Their importare wrong. What you wanted was this:

package resource;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import domain.Album;
import domain.Banda;
import domain.Musica;

@Path("/banda")
public class BandaService {

    @GET
    @Path("/get")
    @Produces("application/xml")
    public Banda getUserInXML() {

        Banda banda = new Banda();
        banda.setNome("Led Zeppelin");

        Album album = new Album();
        album.setAno(1969);
        album.setNome("Led Zeppelin");

        Musica musica1 = new Musica();
        Musica musica2 = new Musica();
        List<Musica> musicas = new ArrayList<Musica>();

        musica1.setNome("Good Times Bad Times");
        musica2.setNome("You Shook Me");
        musicas.add(musica1);
        musicas.add(musica2);

        album.setMusicas(musicas);
        banda.setAlbum(album);

        return banda; 
    }
}
  • IDE Eclipse is criticizing GET, POST and Produces imports!

  • @alexjosesilva Hence is classpath problem. Make sure it finds JAX-RS in classpath.

  • how do I do that ?

  • @alexjosesilva https://stackoverflow.com/q/11302531/540552

  • @alexjosesilva https://stackoverflow.com/q/10869977/540552

  • Error still persists

  • I posted an image to illustrate the error

  • I posted the web.XML ?

  • I managed to fix : Import the jar file: javax.ws.rs-api-2. 0.jar And add it to java build path. The Issue will be resolved in windows

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.