Tag: jersey

使用Jersey / JAXB / Jackson的Java.util.Map到JSON对象

我一直在尝试创建Jersey REST Web服务。 我想从Java类接收和发出JSON对象,如下所示: @XmlRootElement public class Book { public String code; public HashMap names; } 这应该像这样转换为JSON: { “code”: “ABC123”, “names”: { “de”: “Die fabelhafte Welt der Amelie”, “fr”: “Le fabuleux destin d’Amelie Poulain” } } 但是我找不到一个标准的解决方案。 每个人似乎都在实施自己的包装 解决方案 。 这个要求对我来说似乎是极其基本的; 我无法相信这是普遍接受的解决方案,特别是因为Jersey真的是Java中更有趣的部分之一。 我也试过升级到Jackson 1.8,它只给了我这个,这是极端虚假的JSON: { “code”: “ABC123”, “names”: { “entry”: [{ “key”: “de”, “value”: “Die […]

尝试将文件上传到JAX-RS(泽西岛)服务器

我正在尝试使用带有Jersey的multipart / form-data客户端上传文件和其他表单数据。 我也使用Jersey上传到REST Web服务。 这是服务器代码: @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public String create(@FormDataParam(“file”) InputStream file, @FormDataParam(“file”) FormDataContentDisposition fileInfo, @FormDataParam(“name”) String name, @FormDataParam(“description”) String description) { Ingredient ingredient = new Ingredient(); ingredient.setName(name); ingredient.setDescription(description); ingredient.setImageName(fileInfo.getFileName()); ingredient.setImagePath(context.getRealPath(“/resources/uploads/”)); // TODO save the file. try { JSONObject json = new JSONObject(); try { ingredientService.create(ingredient); } catch (final InvalidParameterException ex) { logger.log(Level.INFO, […]

获取错误找不到Java类java.util.ArrayList / List 的消息正文编写器

这已经在这里发布了很多时间,但没有解决方案对我有用… 我可以通过创建一个包装类来避免这个错误,但它只返回 我究竟做错了什么 ? StringWrapper类: import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class StringWrapper { public StringWrapper (){} List list=new ArrayList(); public void add(String s){ list.add(s); } } 代码: @Path(“/xml”) @GET @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}) public StringWrapper mystring(){ StringWrapper thestring=new StringWrapper(); thestring.add(“a”); thestring.add(“a”); return thestring; } 使用Jersey的Java Rest webservice。

阅读jersey的ParamConverter中的另一个参数

我已经制作了一个ParamConverter ,当给定一个格式化为Instant的原生ISO-8601的字符串时,它提供一个Instant (Date),或者作为epoch以来的整数毫秒。 这工作正常,但我还需要能够支持其他日期格式(客户很挑剔)。 为了避免经典的dd/mm/yyyy与mm/dd/yyyy歧义,我想让客户指定他们的首选格式作为请求的一部分*。 例如: GET http://api.example.com/filter?since=01/02/2000&dateformat=dd/mm/yyyy 传递给一个看起来像这样的方法: @GET String getFilteredList( final @QueryParam( “since” ) Instant since ) { … } (为清楚起见省略了时间和时区部分) 所以我希望我的ParamConverter能够读取dateformat参数。 我已经能够使用设置ContainerRequestContext属性的filter和AbstractValueFactoryProvider的组合来做类似的事情,但是这需要参数来应用自定义注释,并且不允许它与QueryParam / FormParam / etc一起使用。 ,使它远没那么有用。 有没有办法从ParamConverter中获取其他参数或请求对象本身? [*]在现实世界中,这将来自一系列预先批准的格式,但现在只是假设它们为DateTimeFormatter提供输入 为清楚起见,这是我的代码: public class InstantParameterProvider implements ParamConverterProvider { private static final ParamConverter INSTANT_CONVERTER = new ParamConverter( ) { @Override public final T fromString( final […]

无法在Jersey中进行基本的http身份validation

我正在尝试使用Jersey 1.X版本连接到安全的外部rest服务。 我使用了以下代码 public class MyRestClient { private static final String API_USER_NAME = “some value”; private static final String API_PASSWORD = “some value”; private static final String REST_URL = “https://”; public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); client.addFilter(new HTTPBasicAuthFilter(API_USER_NAME, API_PASSWORD)); WebResource webResource = client.resource(UriBuilder.fromUri(REST_URL).build()); ClientResponse response = […]

如何在Jersey 2中修改QueryParam和PathParam

我正在尝试过滤/修改Post和Put调用,以确保从HTML和JS代码中过滤用户提供的所有参数,以防止XSS攻击。 我想确保这是在API级别实现的,因此无论使用何种客户端,它都将受到保护。 使用Jersey 1.x,可以通过实现ContainerRequestFilter并在与请求的servlet匹配之前修改request.getQueryParameters()来实现。 示例: http : //codehustler.org/blog/jersey-cross-site-scripting-xss-filter-for-java-web-apps/ 然而,对于Jersey 2,由于我们不能再使用getParameters()或getPathParameters(),因此无法通过实现相同的接口实现这一点,但是我们只能使用getUriInfo(),但由于查询参数是不可变的,因此它是无用的。 我查看了Jersey的filter和拦截器,但遗憾的是它们只能访问标题和Cookie。 我花了很多时间研究,但我找不到我想要的东西。 是否有另一种方法来过滤路径和查询参数? 有什么我想念的吗? 谢谢!

如何使用JAXB将属性对象转换为JSON对象

我想接受并响应REST应用程序中的JSON对象。 我需要发送和接收的数据位于.properties文件中。 我已经阅读过它们,现在它们属于一个Properties对象(来自java.util.Properties )。 有没有一种方法可以编组和解组Properties对象,而无需实现新类? 我在Weblogic服务器中使用Jax-rs API。 @POST @Path(“{id}”) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public JSONObject getbyID(@PathParam(“id”)JSONObject inputJsonObj) { //marshalling and unmarshalling goes here }

如何使用Jersey API从宁静的Web服务发送和接收JSON数据

@Path(“/hello”) public class Hello { @POST @Path(“{id}”) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public JSONObject sayPlainTextHello(@PathParam(“id”)JSONObject inputJsonObj) { String input = (String) inputJsonObj.get(“input”); String output=”The input you sent is :”+input; JSONObject outputJsonObj = new JSONObject(); outputJsonObj.put(“output”, output); return outputJsonObj; } } 这是我的webservice(我正在使用Jersey API)。 但我无法找到一种方法从java rest客户端调用此方法来发送和接收json数据。 我尝试了以下方式来编写客户端 ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); […]

REST Webservice返回415 – 不支持的媒体类型

我使用jax-rs和jersey创建了一个REST Web服务,它应该在POST请求中使用JSON。 我的Web服务类看起来像这样: @Path(“/webhookservice”) public class Webhook { @POST @Consumes(MediaType.APPLICATION_JSON) public Response readData (Song song) { // Prints out the song info System.out.println(“SONG INFO \n=======================”); System.out.println(“songname: ” + song.getSongname()); System.out.println(“artist: ” + song.getArtist()); // Repsonse with a HTTP 200 OK Response response = Response.status(200).build(); return response; } } 我的歌class: public class Song { private String […]

JAXB @XmlElements,不同类型但同名?

我有一个Animal类和Animal的扩展名为AnimalExtension。 public class Animal public class AnimalExtension extends Animal 这两个类之间的唯一区别是AnimalExtension有另一个名为animalId的实例变量。 Animal没有这个实例变量。 我也有自己的数据类型,我想编组和解组XML。 此数据类型称为AnimalList。 在AnimalList中,有一个动物列表作为实例变量。 @XmlType(name = “AnimalList”) public class AnimalList{ private List animalList; …. animalList可以包含Animal和AnimalExtension。 但是,在XML上我不希望该元素被命名为AnimalExtension; 我希望他们都拥有Animal的元素名称。 当JAXB知道Animal实际上是AnimalExtension的一个实例时,我只希望显示额外的属性。 所以,如果我有一个列表,看起来像 List animalList = new LinkedList(); AnimalExtension animalExtension = new AnimalExtension(); animalExtension.setAnimalId(1); amimalExtension.setName(“Don”); Animal animal = new Animal(); animal.setName(“Mike”); animalList.add(animalExtension); animalList.add(animal); 我希望XML看起来像 这是我试图做的 @XmlElements( { @XmlElement(name = […]