jersey

Create a text/plain Jersey response

别来无恙 提交于 2019-12-29 06:25:16
问题 I have some code that works, but I am looking for a better way to do it. I have a RESTful web API that I want to support JSON, XML, and TEXT media types. The JSON and XML is easy with a JAXB annotated "bean" class. I just got text/plain to work, but I wish Jersey was a little more intelligent and would be able to convert a list of my beans, List<Machine> to a string using toString . Here is the Resource class. The JSON and XML media types use a JAXB annotated bean class. The text plain uses a

In REST / Java, what should I return if my object is null?

…衆ロ難τιáo~ 提交于 2019-12-29 06:24:53
问题 I have a simple POJO that I annotated with REST annotations as follows: @GET @Path("/domains/{domainid}") @Override public Domain getDomain(@PathParam("domainid") UUID domainID) throws Exception { logger.info("Retrieving domain "+ domainID); Domain d = null; try { d = MyClient.getDomains().get(domainID.toString()); logger.debug("Returning "+d.getName()); } catch (Exception e) { logger.error("Could not retrieve domain", e); } return d; } Note that the log statement including d.getName() can

In REST / Java, what should I return if my object is null?

会有一股神秘感。 提交于 2019-12-29 06:24:01
问题 I have a simple POJO that I annotated with REST annotations as follows: @GET @Path("/domains/{domainid}") @Override public Domain getDomain(@PathParam("domainid") UUID domainID) throws Exception { logger.info("Retrieving domain "+ domainID); Domain d = null; try { d = MyClient.getDomains().get(domainID.toString()); logger.debug("Returning "+d.getName()); } catch (Exception e) { logger.error("Could not retrieve domain", e); } return d; } Note that the log statement including d.getName() can

Jersey REST WS Error: “Missing dependency for method… at parameter at index X”

巧了我就是萌 提交于 2019-12-29 06:16:13
问题 I am getting the following error: Apr 09, 2013 12:24:26 PM com.sun.jersey.spi.inject.Errors processErrorMessages SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.package.ImportService.specifyLocalFile(java.lang.String,java.lang.String,java.lang.String,java.lang.String) at parameter at index 0 SEVERE: Missing dependency for method public javax.ws.rs.core.Response com

How to submit data with Jersey client POST method

只愿长相守 提交于 2019-12-29 04:30:19
问题 I am new to Jersey. I need to implement a Jersey client to submit data with POST method. The curl command is: curl -d '{"switch": "00:00:00:00:00:00:00:01", "name":"flow-mod-1", "priority":"32768", "ingress-port":"1","active":"true", "actions":"output=2"}' http://localhost:8080/wm/staticflowentrypusher/json So I am trying to figure out how to use Jersey client to implement the above curl command. So far I have done: public class FLClient { private static Client client; private static

Can I send an excel file and JSON body with a description of file in same REST API Response

时光毁灭记忆、已成空白 提交于 2019-12-29 02:00:51
问题 I have an API which returns APPLICATION_OCTET_STREAM as Media Type in response. I need to enhance it to also send a JSON body with some details regarding the file, say counts of right and wrong records in the file. So basically I need two kinds of response in same API. Is this doable ? 回答1: It's possible, but you will need to use a Multipart response. Keep in mind though that some clients will not be able to handle this type of response. You'll normally see this data type using in uploading

jersey2 Unit testing,HttpServletRequest is null

自作多情 提交于 2019-12-29 01:45:07
问题 Ask everybody to help? jersey Bug connection: [1]: https://java.net/jira/browse/JERSEY-2412 The servlet request, response and context not injected into the class when I using test provider (tested jetty and grizzly2). I using packages annotation to pull up the application. Do you have any other way? public class VMResourceTest extends BaseTest { @Test public void testCreateVm() { String bodyData = loadClassPathData(CLASS_PATH+File.separator+"tools"+File.separator+"createVm.json"); Response

How does ServiceLocator find @Service and @Contact automatically in HK2?

♀尐吖头ヾ 提交于 2019-12-28 23:59:07
问题 According to HK2 @Service javadoc Annotation placed on classes that are to be automatically added to an hk2 ServiceLocator. I don't know how to make ServiceLocator find annotated classes automatically. TestService @Contract public interface TestService { } TestServiceImpl @Service public class TestServiceImpl implements TestService { } Main public static void main(String[] args) { ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator(); TestService service = locator

How to set to default to json instead of xml in jersey?

空扰寡人 提交于 2019-12-28 15:25:10
问题 Using jersey jersey.java.net How do I set JSON as the default serialization instead of XML when there is no accept header or .xml suffix is in the URI? 回答1: You can assign the quality index to each media type in @Produces annotation. I.e.you can do the following to make Jersey prefer JSON if both XML and JSON are allowed: @Produces({"application/json;qs=1", "application/xml;qs=.5"}) 回答2: You should be able to set the @Produces annotation to specify the return format like so: @Produces( {

JAX-RS (Jersey) custom exception with XML or JSON

亡梦爱人 提交于 2019-12-28 08:09:35
问题 I have a REST service built using Jersey. I want to be able to set the MIME of my custom exception writers depending on the MIME that was sent to the server. application/json is returned when json is received, and application/xml when xml is received. Now I hard code application/json , but that's making the XML clients left in the dark. public class MyCustomException extends WebApplicationException { public MyCustomException(Status status, String message, String reason, int errorCode) { super