tags:

views:

98

answers:

2

Hi , I am using cxf as a webservice.It supports xml and json format output of the requested data.I want to know that if some exception has occured in my code then i want to return him back the error code either in xml or json format.But i dont know when to give json and xml ,it depends on the requested url that user has asked.

example

@Path("/reports/ad-view/loginId/{loginId}/publisher/")

PublisherReports getPublisherReportsAdView(

  @PathParam("loginId") String loginId,
  @QueryParam("fromDate") String fromDate,
  @QueryParam("toDate") String toDate,
  @QueryParam("filterValue") String filterValue);
+1  A: 

If you mean you want to detect the mime type used to make the request, then you can use the @Consumes annotation to dictate which method handles which type of request. So you could write:

// Called when an XML request is made
@Path("/reports/ad-view/loginId/{loginId}/publisher/")
@Consumes("application/xml")
PublisherReports getPublisherReportsAdViewXml(...

and:

// Called when a JSON request is made
@Path("/reports/ad-view/loginId/{loginId}/publisher/")
@Consumes("application/json")
PublisherReports getPublisherReportsAdViewJson(...

Then have each variant of the getPublisherReportsAdView() method call a common method to perform the actual processing logic but still handle exceptions differently depending on the method that gets called.

Andy
in this case i m making extra function which will increase my code size.Instead of that i can pass the query param which will tell me mimetype as xml or json.And i dont need to know mimeType if there is no exception in my code.If everything is fine then cxf will automatically return the requested format
ha22109
A: 

An alternative approach, which doesn't require additional methods, would be to add a parameter which is annotated with the @HeaderParam annotation and use that to hold the value of the 'Content-Type' request header.

e.g.:

PublisherReports getPublisherReportsAdView(
                @PathParam("loginId") String loginId,
                @QueryParam("fromDate") String fromDate,
                @QueryParam("toDate") String toDate,
                @QueryParam("filterValue") String filterValue,
                @HeaderParam("Content-Type") String contentType)
{
  ...

The value of contentType will likely also include charset information, for example: application/json; charset=UTF-8 so you'll need to ignore that when working out if the request contained JSON or XML.

Andy
how to define headerparam in the urlcan u explain little more
ha22109
if i print content-type it is giving value 'null'
ha22109
@HeaderParam doesn't inject a value from the URL, it takes a value from an HTTP header, with the given name, in the request. Here is the documentation for it: http://jackson.codehaus.org/javadoc/jax-rs/1.0/javax/ws/rs/HeaderParam.html
Andy
i m not getting it
ha22109
Do you mean you don't understand, or you aren't seeing a value being injected when using @HeaderParam?Perhpas check the HTTP request being made to be sure that the header is present?
Andy
got it.But i m not setting the header.there are two aap one aap will call another.I think @consumes will be better in this case
ha22109