2014-11-02 7 views
6

Una volta ho avuto questa Domanda Ultime Jersey esempio non funziona risposto, mi imbatto in un altro problema curioso:MediaType.APPLICATION_XML e MediaType.APPLICATION_JSON in un'applicazione demo Jersey

Il server, GET metodi funzionano bene. Ho testato e aggiunto un codice di prova per l'esempio helloworld-pure-jax-rs e in particolare aggiunto una richiesta POST per JSON:

package org.glassfish.jersey.examples.helloworld.jaxrs; 

import javax.ws.rs.Consumes; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 
import javax.ws.rs.core.Response; 


@Path("helloworld") 
public class HelloWorldResource 
{ 
    public static final String CLICHED_MESSAGE = "Hello World!"; 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getHello() 
    { 
     return CLICHED_MESSAGE; 
    } 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public String getHelloJson() 
    { 
     return "{ \"message\":" + CLICHED_MESSAGE + "}"; 
    } 

    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public String getHelloHtml() 
    { 
     return "<html> " + "<title>" + "Hello Jersey" + "</title>" + "<body><h1>" + CLICHED_MESSAGE 
       + "</body></h1>" + "</html> "; 
    } 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    @Path("/v2") 
    public String getHello2() 
    { 
     return CLICHED_MESSAGE + " v2"; 
    } 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    @Path("/{id}") 
    public String getHelloId(@PathParam("id") String id) 
    { 
     return CLICHED_MESSAGE + " Parameter: " + id; 
    } 

    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    @Path("/id/{id : [a-zA-Z][a-zA-Z_0-9]}") 
    public String getHelloIdId(@PathParam("id") String id) 
    { 
     return CLICHED_MESSAGE + " Parameter: " + id; 
    } 

    @POST 
    @Consumes(MediaType.TEXT_PLAIN) 
    @Produces(MediaType.TEXT_PLAIN) 
    public Response test(String test) 
    { 
     if (test.equals("test")) 
      return Response.status(400).entity("Error: " + test).build(); 
     return Response.status(200).entity(test).build(); 
    } 

    @POST 
    @Path("/test") 
    @Consumes(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response testJSON(Test test) 
    { 
     String result = "Test JSON created : " + test.getName() + "" + test.getAge(); 
     // return result; 
     return Response.status(200).entity(result).build(); 
    } 

    @POST 
    @Path("/test") 
    @Consumes(MediaType.APPLICATION_XML) 
    @Produces(MediaType.APPLICATION_XML) 
    public Response testXML(Test test) 
    { 
     String result = "Test XML created : " + test.getName() + "" + test.getAge(); 
     // return result; 
     return Response.status(200).entity(result).build(); 
    } 

} 

Qui ist il resto delle classi:

package org.glassfish.jersey.examples.helloworld.jaxrs; 

import java.io.IOException; 
import java.net.InetSocketAddress; 
import java.net.URI; 

import javax.ws.rs.core.UriBuilder; 
import javax.ws.rs.ext.RuntimeDelegate; 

import com.sun.net.httpserver.HttpHandler; 
import com.sun.net.httpserver.HttpServer; 

/** 
* Hello world application using only the standard JAX-RS API and lightweight 
* HTTP server bundled in JDK. 
* 
* @author Martin Matula (martin.matula at oracle.com) 
*/ 
@SuppressWarnings("restriction") 
public class App 
{ 

    /** 
    * Starts the lightweight HTTP server serving the JAX-RS application. 
    * 
    * @return new instance of the lightweight HTTP server 
    * @throws IOException 
    */ 
    static HttpServer startServer() throws IOException 
    { 
     // create a new server listening at port 8080 
     HttpServer server = HttpServer.create(new InetSocketAddress(getBaseURI().getPort()), 0); 

     // create a handler wrapping the JAX-RS application 
     HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new JaxRsApplication(), 
       HttpHandler.class); 

     // map JAX-RS handler to the server root 
     server.createContext(getBaseURI().getPath(), handler); 

     // start the server 
     server.start(); 

     return server; 
    } 

    public static void main(String[] args) throws IOException 
    { 
     System.out.println("\"Hello World\" Jersey Example Application"); 

     HttpServer server = startServer(); 

     System.out.println("Application started.\n" + "Try accessing " + getBaseURI() 
       + "helloworld in the browser.\n" + "Hit enter to stop the application..."); 
     System.in.read(); 
     server.stop(0); 
    } 

    private static int getPort(int defaultPort) 
    { 
     final String port = System.getProperty("jersey.config.test.container.port"); 
     if (null != port) 
     { 
      try 
      { 
       return Integer.parseInt(port); 
      } 
      catch (NumberFormatException e) 
      { 
       System.out.println("Value of jersey.config.test.container.port property" 
         + " is not a valid positive integer [" + port + "]." 
         + " Reverting to default [" + defaultPort + "]."); 
      } 
     } 
     return defaultPort; 
    } 

    /** 
    * Gets base {@link URI}. 
    * 
    * @return base {@link URI}. 
    */ 
    public static URI getBaseURI() 
    { 
     return UriBuilder.fromUri("http://localhost/").port(getPort(8080)).build(); 
    } 
} 


public class Test 
{ 
    public int  age  = 0; 
    public String name = ""; 

    /** 
    * 
    */ 
    public Test() 
    { 
     super(); 
    } 

    /** 
    * @param age 
    */ 
    public Test(int age) 
    { 
     super(); 
     this.age = age; 
    } 

    /** 
    * @param name 
    */ 
    public Test(String name) 
    { 
     super(); 
     this.name = name; 
    } 

    /** 
    * @param name 
    * @param age 
    */ 
    public Test(String name, int age) 
    { 
     super(); 
     this.name = name; 
     this.age = age; 
    } 

    public int getAge() 
    { 
     return age; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void setAge(int age) 
    { 
     this.age = age; 
    } 

    public void setName(String name) 
    { 
     this.name = name; 
    } 
} 

package org.glassfish.jersey.examples.helloworld.jaxrs; 

import java.util.Collections; 
import java.util.HashSet; 
import java.util.Set; 
import javax.ws.rs.core.Application; 

public class JaxRsApplication extends Application 
{ 
    private final Set<Class<?>> classes; 

    public JaxRsApplication() 
    { 
     HashSet<Class<?>> c = new HashSet<Class<?>>(); 
     c.add(HelloWorldResource.class); 
     classes = Collections.unmodifiableSet(c); 
    } 

    @Override 
    public Set<Class<?>> getClasses() 
    { 
     return classes; 
    } 
} 

Questo funziona bene per il messaggio di pianura posta di testo, ma fpr il JSON (MediaType.APPLICATION_JSON) e XML parte (MediaType.APPLICATION_XML) non riesce a dichiarare non un tipo di supporto supportato. Qualche idea di cosa potrebbe essere sbagliato?

risposta

4

JAX-RS ha un gruppo di gestori incorporati che possono effettuare il marshalling verso e da alcuni tipi di Java specifici specifici.

Una volta iniziato a gestire l'associazione dei dati personalizzati (marshalling/unmarshalling agli oggetti Java), ci troviamo in un gioco di palle diverso. Ora richiediamo altri MessageBodyWriters e MesageBodyReaders.

Fortunatamente, sono già disponibili lettori e scrittori per l'associazione dati XML e JSON. JAX-RS viene fornito con un marshalling/unmarshalling XML standard, con un avvertimento .. dobbiamo usare le annotazioni JAXB. Così, per la classe Test, ammesso che sia come questo

public class Test { 
    private String name; 
    private int age; 

    public String getName() { return name; } 
    public void setName(String name) { this.name = name;} 
    public int getAge() { return age; } 
    public void setAge(int age) { this.age = age; } 
} 

per rendere consentire al fornitore di JAXB a unmarshall/marshall, dovremmo fornire, come minimo, un @XmlRootElement

@XmlRootElement 
public class Test { 
    .... 
} 

In questo modo si dovrebbe consentire l'XML per funzionare.

Per quanto riguarda il JSON, l'associazione JSON non è un par standard della specifica, ma possiamo semplicemente aggiungere una dipendenza al progetto, che registrerà automaticamente il provider necessario per gestire l'associazione JSON. Puoi guardare il pom.xml per l'esempio. Vedrete questa dipendenza necessaria

<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-moxy</artifactId> 
</dependency> 

Che la dipendenza permette l'applicazione di fare, è maresciallo/unmarshalling JSON da/per i nostri oggetti Java, utilizzando le annotazioni JAXB. Quindi aggiungendo questa dipendenza allo pom.xml. L'applicazione dovrebbe funzionare. Appena testato

+0

Grazie per la risposta rapida e molto eccellente. Funziona ora. – user2737950

Problemi correlati