users@jersey.java.net

Re: [Jersey] How to activate the Jersey log?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 29 May 2009 09:39:55 -0700

Hi,

For the way you have designed things you need to ensure that the root
element for each concrete JAXB type is different otherwise JAXB will
not know which specifically to unmarshall if more than one of those
types is present in the JAXBContext.

It seems you are trying to define a "request" envelope. For RESTful
systems you do not require such an envelope and you can let the root
element be descriptive of the actual content.

@XmlRootElement(name="atividade")
@XmlSeeAlso(AtividadeTO.class)
public class AtividadeSmartGWTRequest extends
SmartGWTRequest<AtividadeTO> {

     protected AtividadeSmartGWTRequest() {
     }

     public AtividadeSmartGWTRequest(AtividadeTO d) {
         super(d);
     }

}

@XmlRootElement(name="cortadeira")
@XmlSeeAlso(CortadeiraTO.class)
public class CortadeiraSmartGWTRequest extends
SmartGWTRequest<CortadeiraTO> {

}



The better way to support an envelope is to define one JAXB bean for a
request that uses xsd:any for a child element.

Then you configure your JAXB context to utilize the JAXB request bean
type and any other bean types that may be present for the xsd:any. To
configure the JAXB context you define your own
ContextResolver<JAXBContext> implementation e.g.

   @Provider
   public MyJAXBResolver implements ContextResolver<JAXBContext> {
   }

But the problem with this is you loose the type information in the
request envelope bean.

Paul.


On May 29, 2009, at 9:24 AM, Fabio Oliveira wrote:

> Yes, they have.
>
> The message I'm trying to parse has the same root element, but diff
> content, like the samples:
>
> <request>
> <data>
> <record>
> <id>16757</id>
> <cortadeiraPrincipal>2</cortadeiraPrincipal>
> <sequencia>0</sequencia>
> <papel>
> <id>1050</id>
> <descricao>Pasta</descricao>
> </papel>
> <!--duracao>00:00</duracao-->
> <larguraBobina>766</larguraBobina>
> <diametroBobina>1000</diametroBobina>
> <tubete>76</tubete>
> <grupos>1: 700 x 1200&lt;br /&gt;2: 200 x 1100</grupos>
> <camadas>1</camadas>
> <!--distribuicao>20x1</distribuicao-->
> <sequencia>0</sequencia>
> <cortadeiraPrincipal>2</cortadeiraPrincipal>
> </record>
> </data>
> <dataSource>filaAtividadeDS</dataSource>
> <operationType>update</operationType>
> <operationId>1</operationId>
> <startRow>0</startRow>
> <endRow>0</endRow>
> <sortBy>id</sortBy>
> <textMatchStyle>substring</textMatchStyle>
> <oldValues>test</oldValues>
> </request>
>
> And:
>
> <request>
> <data>
> <record>
> <id>1</id>
> <codigo>CTCT385</codigo>
> </record>
> </data>
> <dataSource>filaAtividadeDS</dataSource>
> <operationType>update</operationType>
> <operationId></operationId>
> <startRow>0</startRow>
> <endRow>0</endRow>
> <sortBy></sortBy>
> <textMatchStyle></textMatchStyle>
> <oldValues></oldValues>
> </request>
>
> Then I wrote a class hierarchy where the root has the common data:
>
> @XmlAccessorType(XmlAccessType.FIELD)
> public abstract class SmartGWTRequest<T> {
>
> private Data<T> data;
>
> private String dataSource;
> private String operationType;
> private String operationId;
> private Integer startRow;
> private Integer endRow;
> private String sortBy;
> private String textMatchStyle;
> private String oldValues;
>
> @XmlAccessorType(XmlAccessType.FIELD)
> protected static class Data<T> {
> @XmlAnyElement(lax=true)
> private T data;
>
> protected Data() {
> }
>
> public Data(T data) {
> this.data = data;
> }
>
> public T getData() {
> return data;
> }
>
>
> }
> // And a lot of getters/setters
> }
>
> @XmlRootElement(name="request")
> @XmlSeeAlso(AtividadeTO.class)
> public class AtividadeSmartGWTRequest extends
> SmartGWTRequest<AtividadeTO> {
>
> protected AtividadeSmartGWTRequest() {
> }
>
> public AtividadeSmartGWTRequest(AtividadeTO d) {
> super(d);
> }
>
> }
>
> @XmlRootElement(name="request")
> @XmlSeeAlso(CortadeiraTO.class)
> public class CortadeiraSmartGWTRequest extends
> SmartGWTRequest<CortadeiraTO> {
>
> }
>
> The subclasses are almost empty because they are the only way I
> could put the generics to work. Is this is the wrong way to go to
> make everything work? Any suggestions?
>
> Thanks very much, now I have something to look for....
>
> On Fri, May 29, 2009 at 12:28 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
> wrote:
>
> On May 29, 2009, at 7:59 AM, Fabio Oliveira wrote:
>
>> Hi Paul,
>>
>> I did your suggestion, and I the return type for the "o" object is
>> a "CortadeiraSmartGWTRequest", another class of mine, but used for
>> another resource and path! Crazy!
>>
>> You have any clues about this?
>>
>
> Do both of those JAXB objects have the same XML root element
> defined, namely "request" ?
>
> When unmarshalling using JAXB the type returned is based on the XML
> root element and the JAXBContext.
>
> Paul.
>
>> Thanks!
>>
>> On Thu, May 28, 2009 at 6:22 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
>> wrote:
>> Hi Fabio,
>>
>> Perhaps you could try the following:
>>
>> @PUT
>> @Consumes(MediaType.TEXT_XML)
>> public SmartGWTResponse putAtividade(@Context HttpContext hc) {
>> Object o =
>> hc.getRequest().getEntity(AtividadeSmartGWTRequest.class);
>> }
>>
>> and see what is returned.
>>
>> I am wondering if the XmlRootElement on SmartGWTRequest is
>> confusing JAXB, or maybe the generic aspect of that class.
>>
>> Paul.
>>
>> On May 28, 2009, at 1:50 PM, Fabio Oliveira wrote:
>>
>>> Hi Paul,
>>>
>>> thanks for your answer. The AtividadeSmartGWTRequest is one of my
>>> types, with JAXB annotations:
>>>
>>> @XmlRootElement(name="request")
>>> @XmlSeeAlso(AtividadeTO.class)
>>> public class AtividadeSmartGWTRequest extends
>>> SmartGWTRequest<AtividadeTO> {
>>>
>>> protected AtividadeSmartGWTRequest() {
>>> }
>>>
>>> public AtividadeSmartGWTRequest(AtividadeTO d) {
>>> super(d);
>>> }
>>>
>>> }
>>>
>>> @XmlAccessorType(XmlAccessType.FIELD)
>>> @XmlRootElement(name = "request")
>>> public abstract class SmartGWTRequest<T> {
>>>
>>> private Data<T> data;
>>>
>>> private String dataSource;
>>> private String operationType;
>>> private String operationId;
>>> private Integer startRow;
>>> private Integer endRow;
>>> private String sortBy;
>>> private String textMatchStyle;
>>> private String oldValues;
>>>
>>>
>>> public SmartGWTRequest(T d) {
>>> this.data = new Data(d);
>>> }
>>>
>>> protected SmartGWTRequest() {
>>> }
>>>
>>> public T getData() {
>>> return data.getData();
>>> }
>>>
>>> public String getDataSource() {
>>> return dataSource;
>>> }
>>>
>>> public Integer getEndRow() {
>>> return endRow;
>>> }
>>>
>>> public String getOldValues() {
>>> return oldValues;
>>> }
>>>
>>> public String getOperationId() {
>>> return operationId;
>>> }
>>>
>>> public String getOperationType() {
>>> return operationType;
>>> }
>>>
>>> public String getSortBy() {
>>> return sortBy;
>>> }
>>>
>>> public Integer getStartRow() {
>>> return startRow;
>>> }
>>>
>>> public String getTextMatchStyle() {
>>> return textMatchStyle;
>>> }
>>>
>>> @XmlAccessorType(XmlAccessType.FIELD)
>>> protected static class Data<T> {
>>> @XmlAnyElement(lax=true)
>>> private T data;
>>>
>>> protected Data() {
>>> }
>>>
>>> public Data(T data) {
>>> this.data = data;
>>> }
>>>
>>> public T getData() {
>>> return data;
>>> }
>>>
>>>
>>> }
>>> }
>>>
>>> @XmlRootElement(name = "record")
>>> public class AtividadeTO {
>>>
>>> private int id;
>>> private PapelTO papel;
>>> private Duration duracao;
>>> private long diametroBobina;
>>> private long larguraBobina;
>>> private long tubete;
>>> private String grupos;
>>> private int camadas;
>>> private DistribuicaoCamadas distribuicao;
>>> private int cortadeiraPrincipal;
>>> private int sequencia;
>>>
>>> protected AtividadeTO() {
>>> }
>>>
>>> public AtividadeTO(Atividade a) {
>>> this.id = a.getId();
>>> this.papel = new PapelTO(a.getPapel());
>>> this.duracao = a.getDuracao();
>>> this.diametroBobina =
>>> a.getGeometriaEntrada().getDiametro().longValue(SI.MILLIMETER);
>>> this.larguraBobina =
>>> a.getGeometriaEntrada().getLargura().longValue(SI.MILLIMETER);
>>> this.tubete =
>>> a
>>> .getGeometriaEntrada
>>> ().getDiametroInterno().longValue(SI.MILLIMETER);
>>> this.grupos = format(a.getGrupos());
>>> this.camadas = a.getCamadas();
>>> this.distribuicao = a.getDistribuicao();
>>> this.cortadeiraPrincipal =
>>> a.getCortadeiraPrincipal().getId();
>>> this.sequencia = a.getSequencia();
>>> }
>>>
>>> // And a lot of getters and setters...
>>>
>>> }
>>>
>>> The message sent to server is:
>>>
>>> <request>
>>> <data>
>>> <record>
>>> <id>16757</id>
>>> <papel>
>>>
>>>
>>> <descricao>PASTA LINTER ALGODAO/F 1,50 MM 2300 MM</
>>> descricao>
>>> <id>1050</id>
>>> </papel>
>>> <duracao>00:00</duracao>
>>>
>>>
>>> <larguraBobina>766</larguraBobina>
>>>
>>> <diametroBobina>1000</diametroBobina>
>>> <tubete>76</tubete>
>>> <grupos>1: 700 x 1200&lt;br /&gt;2: 200 x 1100</grupos>
>>> <camadas>1</camadas>
>>>
>>>
>>>
>>> <distribuicao>20x1</distribuicao>
>>> <sequencia>0</sequencia>
>>> <cortadeiraPrincipal>2</cortadeiraPrincipal>
>>> </record>
>>> </data>
>>>
>>>
>>>
>>> <dataSource>filaAtividadeDS</dataSource>
>>> <operationType>update</operationType>
>>> <operationId></operationId>
>>> <startRow></startRow>
>>> <endRow></endRow>
>>>
>>>
>>>
>>> <sortBy></sortBy>
>>> <textMatchStyle></textMatchStyle>
>>> <oldValues></oldValues>
>>> </request>
>>>
>>>
>>> I don't know if it looks familiar for anyone, but I'm using the
>>> SmartGWT widget toolkit (http://www.smartclient.com/smartgwt) and
>>> trying to integrate with the Jersey, with a Restful architecture.
>>>
>>> I can't figure out where I'm missing the point, everything worked
>>> well until now, but I'm already working against this problem for 2
>>> days, without clues...
>>>
>>> Any help will be appreciated....
>>>
>>>
>>> On Thu, May 28, 2009 at 4:35 PM, Paul Sandoz <Paul.Sandoz_at_sun.com>
>>> wrote:
>>> Hi Fabio,
>>>
>>> Unfortunately there is no logging statements in the code. I want
>>> to enable a much more dynamic approach with the concept of a
>>> developer mode that adapts classes with logging information
>>> rather than "polluting" the runtime code with logging statements.
>>>
>>> What is AtividadeSmartGWTRequest? a JAXB object or your own type?
>>> if the latter i presume you have message body reader for that
>>> type, are you sure the reader is returning an instance compatible
>>> with that type?
>>>
>>> Paul.
>>>
>>> On May 28, 2009, at 11:06 AM, Fabio Oliveira wrote:
>>>
>>>> Hi list!
>>>>
>>>> Is there any way to log the Jersey activity? I'm having problems
>>>> while sending a msg and parsing using Jersey, and I'm receiving
>>>> the message:
>>>>
>>>> java.lang.IllegalArgumentException: argument type mismatch
>>>> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>
>>>> sun
>>>> .reflect
>>>> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>>
>>>> sun
>>>> .reflect
>>>> .DelegatingMethodAccessorImpl
>>>> .invoke(DelegatingMethodAccessorImpl.java:25)
>>>> java.lang.reflect.Method.invoke(Method.java:597)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl.model.method.dispatch.AbstractResourceMethodDispatchProvider
>>>> $
>>>> TypeOutInvoker
>>>> ._dispatch(AbstractResourceMethodDispatchProvider.java:156)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl
>>>> .model
>>>> .method
>>>> .dispatch
>>>> .ResourceJavaMethodDispatcher
>>>> .dispatch(ResourceJavaMethodDispatcher.java:67)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:
>>>> 166)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server.impl.uri.rules.SubLocatorRule.accept(SubLocatorRule.java:
>>>> 108)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:
>>>> 114)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:74)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:
>>>> 114)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl
>>>> .uri
>>>> .rules
>>>> .RootResourceClassesRule.accept(RootResourceClassesRule.java:66)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl
>>>> .application
>>>> .WebApplicationImpl._handleRequest(WebApplicationImpl.java:658)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl
>>>> .application
>>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:616)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .server
>>>> .impl
>>>> .application
>>>> .WebApplicationImpl.handleRequest(WebApplicationImpl.java:607)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .spi.container.servlet.WebComponent.service(WebComponent.java:309)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .spi
>>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>>> 425)
>>>>
>>>> com
>>>> .sun
>>>> .jersey
>>>> .spi
>>>> .container.servlet.ServletContainer.service(ServletContainer.java:
>>>> 590)
>>>> javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
>>>>
>>>> org
>>>> .springframework
>>>> .orm
>>>> .jpa
>>>> .support
>>>> .OpenEntityManagerInViewFilter
>>>> .doFilterInternal(OpenEntityManagerInViewFilter.java:112)
>>>>
>>>> org
>>>> .springframework
>>>> .web
>>>> .filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
>>>>
>>>> javawebparts
>>>> .filter.CompressionFilter.doFilter(CompressionFilter.java:309)
>>>>
>>>> My method signature:
>>>>
>>>> @PUT
>>>> @Consumes(MediaType.TEXT_XML)
>>>> public SmartGWTResponse putAtividade(AtividadeSmartGWTRequest
>>>> request) {
>>>> ...
>>>> }
>>>>
>>>>
>>>> Thanks!
>>>>
>>>> --
>>>>
>>>> Fábio Braga de Oliveira
>>>> ArchitecTeam Consulting
>>>> http://www.linkedin.com/in/fabiobragaoliveira
>>>> E-mail: fabio.braga_at_gmail.com
>>>> Mobile: +55 19 9270-6574
>>>>
>>>
>>>
>>>
>>>
>>> --
>>>
>>> Fábio Braga de Oliveira
>>> ArchitecTeam Consulting
>>> http://www.linkedin.com/in/fabiobragaoliveira
>>> E-mail: fabio.braga_at_gmail.com
>>> Mobile: +55 19 9270-6574
>>>
>>
>>
>>
>>
>> --
>>
>> Fábio Braga de Oliveira
>> ArchitecTeam Consulting
>> http://www.linkedin.com/in/fabiobragaoliveira
>> E-mail: fabio.braga_at_gmail.com
>> Mobile: +55 19 9270-6574
>>
>
>
>
>
> --
>
> Fábio Braga de Oliveira
> ArchitecTeam Consulting
> http://www.linkedin.com/in/fabiobragaoliveira
> E-mail: fabio.braga_at_gmail.com
> Mobile: +55 19 9270-6574
>