我在JBoss AS 6中通过RestEasy使用JAX-RS.当我的JAX-RS资源返回一组项目时(例如通过List),RESTEasy总是使用该名称collection作为根元素.
例如
<collection>
<item>
<description>computer</description>
<price>2500</price>
</item>
<item>
<description>tv</description>
<price>1500</price>
</item>
</collection>
Run Code Online (Sandbox Code Playgroud)
此XML由以下内容生成:
@Produces("application/xml")
@Path("xml")
@RequestScoped
public class MyResource {
@GET
@Path("myitems")
public List<Item> getMyItems() {
return ...
}
}
Run Code Online (Sandbox Code Playgroud)
可以看出,RESTEasy创建的根标记始终是<collection>.
另一方面,泽西岛总是创建一个名称,该名称是列表中包含的元素的复数形式:
<items>
<item>
<description>computer</description>
<price>2500</price>
</item>
<item>
<description>tv</description>
<price>1500</price>
</item>
</items>
Run Code Online (Sandbox Code Playgroud)
我知道可以创建一个包装器类型并返回而不是List,但这是一个相当精细的解决方法,并使代码更复杂.
是否可以轻松指定集合的根标签名称?
我有一个使用JAX-RS Restlet扩展实现的自托管JAX-RS REST服务.
现在我必须提供静态内容,我想知道如何使用JAX-RS.注意,我不知道编译时的物理目录结构.所以,给出一个像这样的URL
http://bla-bla:8182/static/yaba/daba/doo.png
Run Code Online (Sandbox Code Playgroud)
$(ROOT)/yaba/daba/doo.png必须返回该文件,$(ROOT)静态内容根目录在哪里.
是否可以使用纯JAX-RS进行操作?
谢谢.
编辑
在编译时已知:
编译时未知:
是否有可能选择性地确定@JsonFilter注释何时在运行时使用?
当我不提供过滤器时,我收到JsonMappingException异常(见下文).
背景:
我从最近的StackOverflow帖子中了解到,我可以使用@JsonFilter来动态过滤被序列化的bean属性.这非常有效.添加@JsonFilter("apiFilter")到我的域类并在我的jax-rs服务中添加此代码(使用CXF实现)后,我能够动态过滤我的RESTful API返回的属性:
// shortened for brevity
FilterProvider filters = new SimpleFilterProvider().addFilter("apiFilter", SimpleBeanPropertyFilter.filterOutAllExcept(filterProperties));
return mapper.filteredWriter(filters).writeValueAsString(user);
Run Code Online (Sandbox Code Playgroud)
问题是有不同的服务调用,我根本不想应用过滤器.在这些情况下,我想返回整个域类而不过滤任何属性.在我只是尝试返回域类的情况下,我得到一个例外,如下所示:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not resolve BeanPropertyFilter with id 'apiFilter'; no FilterProvider configured
at org.codehaus.jackson.map.ser.BeanSerializer.findFilter(BeanSerializer.java:252)
at org.codehaus.jackson.map.ser.BeanSerializer.serializeFieldsFiltered(BeanSerializer.java:216)
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:140)
Run Code Online (Sandbox Code Playgroud) 有这样的方法:
@GET @Path("/name/{name}")
@Produces(MediaType.TEXT_PLAIN)
public String getProperty(@PathParam("name") String name) {
System.out.println(name);
}
Run Code Online (Sandbox Code Playgroud)
如何传递"test./test"之类的值?
/name/test./test gives HTTP 404
/name/test.%2Ftest gives HTTP 400
/name/test.%252Ftest prints test%2Ftest
Run Code Online (Sandbox Code Playgroud)
但是,如果我做name = URLDecoder.decode(name);它打印/test,第一部分test.消失.
有一两个这样的问题,但它们已经老了,没有找到好的解决方案,我想我会再问一遍.
public class ShoppingApplication extends Application {
private Set<Object> singletons = new HashSet<>();
private Set<Class<?>> classes = new HashSet<>();
public ShoppingApplication() {
classes.add(CustomerResourceService.class);
classes.add(JAXBMarshaller.class);
classes.add(JSONMarshaller.class);
singletons.add(new CustomerResourceService());
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
Run Code Online (Sandbox Code Playgroud)
假设我有上面的代码,我扩展了应用程序并注册我的资源或提供程序来设置.我想知道如何动态注入我的资源以在运行时设置,我的Web应用程序将在运行时创建几个新资源,并需要注入到Application以便使用.
我使用"Jersey测试框架"对我的webservice进行单元测试.
这是我的资源类:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class class HelloWorldResource {
private SomeService service;
@GET
@Produces("text/plain")
public String getClichedMessage() {
// Return some cliched textual content
String responseFromSomeService = service.getSomething();
return responseFromSomeService;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在单元测试中模拟SomeService?
我有一个使用以下方法的会话Bean:
@POST
@Consumes("application/x-www-form-urlencoded")
@Path("/calculate")
@Produces("application/json")
public CalculationResult calculate(@FormParam("childProfile") String childProfile,
@FormParam("parentProfile") String parentProfile) {
...
}
Run Code Online (Sandbox Code Playgroud)
返回的CalculationResult无法映射到JSON,并发生以下异常:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.test.UniqueName and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)...
Run Code Online (Sandbox Code Playgroud)
如何SerializationFeature在Wildfly中配置Jackson及其?
如何使用JAX-RS获取服务器的基本URL?基本上我想要""http:// localhost:8080/.."当程序在localhost和"http://www.theSite.com/..."当程序在实时服务器上时.我正在使用泽西框架.
我有两个REST类用于简单的Web服务(Jersey和GlassFish),它涉及用户资源 - 一个用于操作所有用户(例如,@POSTing的工厂),另一个用于个人用户(例如@GET,@ PUT,@删除).他们在:
@Stateless @Path("users") public class AllUsersResource {...}
@Stateless @Path("user") public class OneUserResource {...}
Run Code Online (Sandbox Code Playgroud)
分别.在POST到AllUsersResource时,我想返回Response.created(uri).build()新用户的位置(通过),例如,
http://localhost:8080/.../user/152
Run Code Online (Sandbox Code Playgroud)
我的问题是如何做到这一点.AllUsersResource注入了@Context UriInfo uriInfo,但这并没有让我获得OneUserResource的@Path信息,只获得当前调用的信息("users").我最终使用它的方式只是使用反射,但我担心它是脆弱和不洁的:
OneUserResource.class.getAnnotation(Path.class).value();
Run Code Online (Sandbox Code Playgroud)
搜索StackOverflow我发现尝试的其他事情如下,但没有成功:
任何帮助都会很棒!
我正在尝试将我的json请求解析为我的模型.我不知道这段代码有什么问题.json的语法在Java模型上看起来也是正确的和注释.我不知道为什么我会收到如下错误:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of ParametersType out of START_ARRAY token
(through reference chain: Document["parameters"])
Run Code Online (Sandbox Code Playgroud)
Java模型:
@JsonIgnoreProperties( ignoreUnknown = true )
public class Document {
@XmlElement( required = true )
@JsonProperty( "templateId" )
protected String templateId;
@JsonProperty( "parameters" )
@XmlElement( required = true )
protected ParametersType parameters;
@JsonProperty( "documentFormat" )
@XmlElement( required = true )
protected DocumentFormatType documentFormat;
...}
@JsonIgnoreProperties( ignoreUnknown = true )
public class ParametersType {
@JsonProperty( "parameter" )
protected List<ParameterType> parameter;
...}
@JsonIgnoreProperties( ignoreUnknown …Run Code Online (Sandbox Code Playgroud)