Paul,
I was able to find a way to get the requested content type, not sure it's a
best practice though. The HttpHeaders.getMediaType() is null but the List,
HttpHeaders.getAcceptableMediaTypes(), only has the chosen MediaType, so it
seems to work. Any case this wont work for or is there a better way?
@GET
@Path("/topspamsenderchart1")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getTopSpamSenderChartTest1(@Context HttpHeaders headers)
throws Exception{
MediaType mediaType = headers.getAcceptableMediaTypes().get(0);
return Response.ok(chartSpec).type(mediaType).build();
}
cgswtsu78 wrote:
>
> Thanks a bunch Paul, great stuff! I upgraded to 1.1.5. One last thing I
> don't understand is how I can set the repsonses content type to the one
> requested.
>
> I'm doing a:
>
> GET /path/testMethod.json
>
> and I get to the resource's testMethod, but I'm unsure how to populate the
> type property in the Response object. Right now I'm hard coding it as
> below and that will work fine if the request is .json but not for .xml.
> Any ideas?
>
>
> return Response.ok(chartSpec).type("application/json").build();
>
>
>
>
> Paul Sandoz wrote:
>>
>> Hi,
>>
>> Your web.xml is incorrect for registering the ResourceConfig
>> implementation. See:
>>
>> https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html
>> #d4e194
>>
>> your init param need to be something like:
>>
>> <init-param>
>> <param-name>javax.ws.rs.Application</param-name>
>> <param-
>> value>com.proofpoint.resources.chartsvcs.impl.MyResourceConfig</param-
>> value>
>> </init-param>
>>
>> where the value of "javax.ws.rs.Application" declares the fully
>> qualified class name to your implementation of ResourceConfg.
>>
>> If you want to retain support for package scanning you can inherit
>> from PackagesResourceConfig [1], for example:
>>
>> public class MyResourceConfig extends PackagesResourceConfig {
>> public PackagesResourceConfig(Map<String, Object> props) {
>> super(props);
>>
>> // add more config stuff here
>> }
>> }
>>
>> with the following params:
>>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.packages</param-name>
>> <param-value>com.proofpoint.resources.chartsvcs.impl</param-
>> value>
>> </init-param>
>> <init-param>
>> <param-name>javax.ws.rs.Application</param-name>
>> <param-
>> value>com.proofpoint.resources.chartsvcs.impl.MyResourceConfig</param-
>> value>
>> </init-param>
>>
>>
>> Behavior is fully documented here:
>>
>>
>> https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html
>>
>>
>> Alternatively if you are using 1.1.5-ea-SNAPSHOT you can use the
>> following configuration:
>>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.packages</param-name>
>> <param-value>com.proofpoint.resources.chartsvcs.impl</param-
>> value>
>> </init-param>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.MediaTypeMappings</
>> param-name>
>> <param-value>json : application/json</param-value>
>> </init-param>
>>
>> and you do not require to define your own application class. See:
>>
>>
>> https://jersey.dev.java.net/nonav/apidocs/1.1.5-ea-SNAPSHOT/jersey/com/sun/jersey/api/core/ResourceConfig.html
>> #PROPERTY_MEDIA_TYPE_MAPPINGS
>>
>> Paul.
>>
>> [1]
>> https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/core/PackagesResourceConfig.html
>>
>> On Dec 17, 2009, at 6:36 PM, cgswtsu78 wrote:
>>
>>>
>>> Ok thanks, I'll try that. Below is my web.xml
>>>
>>> <?xml version="1.0"?>
>>> <web-app version="2.5"
>>> xmlns="http://java.sun.com/xml/ns/javaee"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
>>> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
>>> <display-name>Greeter</display-name>
>>>
>>> <context-param>
>>> <param-name>webAppRootKey</param-name>
>>> <param-value>ChartWebWiz</param-value>
>>> </context-param>
>>>
>>> <context-param>
>>> <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
>>> <param-value>server</param-value>
>>> </context-param>
>>>
>>> <context-param>
>>> <param-name>org.richfaces.SKIN</param-name>
>>> <param-value>blueSky</param-value>
>>> </context-param>
>>>
>>> <context-param>
>>> <param-name>org.richfaces.CONTROL_SKINNING</param-name>
>>> <param-value>enable</param-value>
>>> </context-param>
>>>
>>> <context-param>
>>> <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
>>> <param-value>.xhtml</param-value>
>>> </context-param>
>>>
>>> <filter>
>>> <display-name>RichFaces Filter</display-name>
>>> <filter-name>richfaces</filter-name>
>>> <filter-class>org.ajax4jsf.Filter</filter-class>
>>> </filter>
>>>
>>> <filter-mapping>
>>> <filter-name>richfaces</filter-name>
>>> <servlet-name>Faces Servlet</servlet-name>
>>> <dispatcher>REQUEST</dispatcher>
>>> <dispatcher>FORWARD</dispatcher>
>>> <dispatcher>INCLUDE</dispatcher>
>>> </filter-mapping>
>>>
>>> <listener>
>>> <listener-class>com.sun.faces.config.ConfigureListener</listener-
>>> class>
>>> </listener>
>>>
>>>
>>> <listener>
>>> <listener-class>
>>>
>>> org
>>> .eclipse
>>> .birt.chart.viewer.internal.listener.ChartServletContextListener
>>> </listener-class>
>>> </listener>
>>> <listener>
>>> <listener-class>
>>>
>>> org
>>> .eclipse.birt.chart.viewer.internal.listener.ChartHttpSessionListener
>>> </listener-class>
>>> </listener>
>>>
>>> <!-- Faces Servlet -->
>>> <servlet>
>>> <servlet-name>Faces Servlet</servlet-name>
>>> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
>>> <load-on-startup>1</load-on-startup>
>>> </servlet>
>>>
>>> <servlet>
>>> <servlet-name>Jersey Web Application</servlet-name>
>>>
>>> <servlet-
>>> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
>>> class>
>>> <init-param>
>>> <param-name>com.sun.jersey.config.property.packages</param-
>>> name>
>>> <param-value>com.proofpoint.resources.chartsvcs.impl</param-
>>> value>
>>> </init-param>
>>> <load-on-startup>1</load-on-startup>
>>> </servlet>
>>> <servlet-mapping>
>>> <servlet-name>Jersey Web Application</servlet-name>
>>> <url-pattern>/jersey/*</url-pattern>
>>> </servlet-mapping>
>>>
>>> <!-- Faces Servlet Mapping -->
>>> <servlet-mapping>
>>> <servlet-name>Faces Servlet</servlet-name>
>>> <url-pattern>*.xhtml</url-pattern>
>>> </servlet-mapping>
>>>
>>> <login-config>
>>> <auth-method>BASIC</auth-method>
>>> </login-config>
>>> </web-app>
>>>
>>> Paul Sandoz wrote:
>>>>
>>>>
>>>> On Dec 17, 2009, at 5:53 PM, cgswtsu78 wrote:
>>>>
>>>>>
>>>>> Paul,
>>>>>
>>>>> I just extended the ResourceConfig class from the jersey-server.jar,
>>>>> do I
>>>>> have to implement a custom version of it?
>>>>>
>>>>
>>>> I recommend extending from DefaultResourceConfig, as you have to
>>>> implement less.
>>>>
>>>> I want to look at the code and the web.xml to see if there is a
>>>> mistake. Feel free to send it me privately if appropriate.
>>>>
>>>> Paul.
>>>>
>>>>
>>>>>
>>>>> Paul Sandoz wrote:
>>>>>>
>>>>>>
>>>>>> On Dec 16, 2009, at 9:03 PM, cgswtsu78 wrote:
>>>>>>
>>>>>>>
>>>>>>> Paul,
>>>>>>>
>>>>>>> Thanks for the suggestions. I tried them but the GET request
>>>>>>> didn't
>>>>>>> find
>>>>>>> the resource.
>>>>>>>
>>>>>>> I extended the ResourceConfig class and added the .json to the end
>>>>>>> of the
>>>>>>> GET request.
>>>>>>>
>>>>>>
>>>>>> Can you share your code for the extended ResourceConfig class and
>>>>>> the
>>>>>> web.xml?
>>>>>>
>>>>>> Paul.
>>>>>>
>>>>>>
>>>>>>
>>>>>>> when I execute GET /chartsvcs/topspamsenderchart the request
>>>>>>> succeeds but
>>>>>>> when I do the below it fails to find it.
>>>>>>>
>>>>>>> executed GET /chartsvcs/topspamsenderchart.json
>>>>>>>
>>>>>>
>>>>>>> @Path("/chartsvcs")
>>>>>>> public class TopSpamSenderWSImpl extends ResourceConfig {
>>>>>>> {
>>>>>>>
>>>>>>> @GET
>>>>>>> @Path("/topspamsenderchart")
>>>>>>> @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
>>>>>>> public Response getTopSpamSenderChartTest1() throws Exception{
>>>>>>>
>>>>>>> Object o = call something to populate object...
>>>>>>>
>>>>>>> return Response.ok(o).type("application/json").build();
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Samuel Le Berrigaud-3 wrote:
>>>>>>>>
>>>>>>>> You might want to use the javax.ws.rs.core.Response class,
>>>>>>>>
>>>>>>>> @GET
>>>>>>>> @Path("/topspamsenderchart")
>>>>>>>> @Produces({MediaType.APPLICATION_XML,
>>>>>>>> MediaType.APPLICATION_JSON})
>>>>>>>> public Response getTest()
>>>>>>>> {
>>>>>>>>
>>>>>>>> Response
>>>>>>>> .ok(something.fetch).type(calculateContentType()).build();
>>>>>>>> }
>>>>>>>>
>>>>>>>> HTH,
>>>>>>>> SaM
>>>>>>>>
>>>>>>>> On Wed, Dec 16, 2009 at 9:34 AM, cgswtsu78 <cgray_at_proofpoint.com>
>>>>>>>> wrote:
>>>>>>>>>
>>>>>>>>> Hello,
>>>>>>>>>
>>>>>>>>> Does anyone know how I can use a querystring parameter to set
>>>>>>>>> the
>>>>>>>>> content
>>>>>>>>> type of the request.
>>>>>>>>>
>>>>>>>>> For example if I have a resource method that accepts both XML
>>>>>>>>> and
>>>>>>>>> JSON,
>>>>>>>>> how
>>>>>>>>> can I let the consumer (via a url GET request) decide what
>>>>>>>>> content
>>>>>>>>> type
>>>>>>>>> they
>>>>>>>>> want to be returned? Is there an annotation I can use in
>>>>>>>>> order to
>>>>>>>>> map a
>>>>>>>>> querystring request parameter to dynamically set the requests
>>>>>>>>> content
>>>>>>>>> type?
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> @GET
>>>>>>>>> @Path("/topspamsenderchart")
>>>>>>>>> @Produces({MediaType.APPLICATION_XML,
>>>>>>>>> MediaType.APPLICATION_JSON})
>>>>>>>>> public Object getTest()
>>>>>>>>> {
>>>>>>>>>
>>>>>>>>> Object o = something.fetch;
>>>>>>>>>
>>>>>>>>> return o;
>>>>>>>>> }
>>>>>>>>> --
>>>>>>>>> View this message in context:
>>>>>>>>> http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4172634.html
>>>>>>>>> Sent from the Jersey mailing list archive at Nabble.com.
>>>>>>>>>
>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> View this message in context:
>>>>>>> http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4177691.html
>>>>>>> Sent from the Jersey mailing list archive at Nabble.com.
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>>
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>> View this message in context:
>>>>> http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4182230.html
>>>>> Sent from the Jersey mailing list archive at Nabble.com.
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4182464.html
>>> Sent from the Jersey mailing list archive at Nabble.com.
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>>
>
>
--
View this message in context: http://n2.nabble.com/Setting-Request-Content-Type-Dynamically-tp4172634p4183963.html
Sent from the Jersey mailing list archive at Nabble.com.