有没有办法使用此参数样式:
/产品/ 123; 456; 789
在泽西的JAX-RS?如果我使用PathParam,则只返回列表中的第一个参数.我试图逃脱分号,但泽西只返回"123; 456; 789"作为第一个参数列表条目的值
我将GET方法声明为
public List<Product> getClichedMessage(@PathParam("ids") List<String> idList)
Run Code Online (Sandbox Code Playgroud)
更新:我指的是Jersey 1.1.5 的泽西用户指南:
通常,方法参数的Java类型可以是(...)4)是List,Set或SortedSet,其中T满足上面的2或3.生成的集合是只读的.(...)有时参数可能包含同一个名称的多个值.如果是这种情况,则可以使用4)中的类型来获得所有值.
更新:这是我的测试代码:
package de.betabeans.resources;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("/test")
public class TestResource {
@GET
@Path("/{ids}")
@Produces({"text/plain"})
public String getClichedMessage(@PathParam("ids") List<String> idList) {
return "size=" + idList.size();
}
}
Run Code Online (Sandbox Code Playgroud)
使用分号转义的测试网址:http:// localhost:8080/resources/test/1%3B2%3B3
更新:Jersey 1.3的更改日志包含以下信息:
修复了问题540
http://java.net/jira/browse/JERSEY-540 参数支持参数化类型的List/Set/SortedSet,例如@QueryParam("d")List>,如果有一个StringReaderProvider注册了支持类型List.
我会在此基础上后退房StringReaderProvider http://comments.gmane.org/gmane.comp.java.jersey.user/7545
在Java中我使用的时候
@Produces("application/json")
Run Code Online (Sandbox Code Playgroud)
注释输出不会形成人类可读的形式.我怎么做到这一点?
我使用Jersey来实现JAX-RS REST样式服务以及用于JSON映射的Jackson 2.0.2.其中一个REST服务返回一个List<EntityA>(让我们称之为indexA)EntityA包含List<EntityB>另一个服务,而另一个服务只返回一个List<EntityB>(让我们称之为indexB):
@Entity
@JsonAutoDetect
public class EntityA {
@Id
private String id;
@OneToMany
private List<EntityB> b;
...
}
@Entity
@JsonAutoDetect
@JsonFilter("bFilter")
public class EntityB {
@Id
private String id;
private String some;
private String other;
private String attributes;
...
}
@Path("/a")
public class AResource {
@GET
@Path("/")
public List<EntityA> indexA() {
...
}
}
@Path("/b")
public class BResource {
@GET
@Path("/")
public List<EntityB> indexB() {
...
} …Run Code Online (Sandbox Code Playgroud) 我尝试访问一个开放的数据Web服务,它为我提供了流量信息.文档说请求必须是GET并且需要包含Accept: application/json和Content-Type: application/json.我不明白为什么他们需要Content-Type但是确定:
我试图只用Accept:Header 检索数据,但我总是得到一个415 Unsupported Media Type.现在我正在尝试这种方式(但我不确定我是否真的正确设置两个标头):
String entity = ClientBuilder.newClient().target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.get(String.class);
Run Code Online (Sandbox Code Playgroud)
如你所见,我正在使用Jersey 2.2而且我还在使用415 Unsupported Media Type.
编辑
所以我开始工作,但我不明白为什么.是不是accept(MediaType.APPLICATION_JSON)也header("Content-type","application/json")一样?
String responseEntity = ClientBuilder.newClient()
.target(liveDataURI)
.path(liveDataPath)
.request(MediaType.APPLICATION_JSON)
.header("Content-type", "application/json")
.get(String.class);
Run Code Online (Sandbox Code Playgroud) 我读到在请求中传递数组的HTTP方法是多次设置一个参数:
1) GET /users?orderBy=last_name&orderBy=first_name
Run Code Online (Sandbox Code Playgroud)
但是,我也看到了逗号分隔的参数(我觉得这是"更干净"):
2) GET /users?orderBy=last_name,first_name
Run Code Online (Sandbox Code Playgroud)
我想实现多排序(通过last_name排序用户,然后复制last_names由first_name排序).代码方面,这很容易(谷歌的番石榴图书馆拯救),但我应该如何揭露这个?第一种方法是否保留字段的顺序(按last_name排序,然后按first_name排序)?
如果在请求中多次设置,Spring会将参数神奇地转换为String []数组:
... @RequestParam("orderBy") String[] orderBy ... becomes ["last_name","first_name"]
Run Code Online (Sandbox Code Playgroud)
这让我相信第一种方式被认为是最佳实践,尽管我喜欢第二种方式......
在我发现的关于JAX-RS和缓存的少数几个问题(有答案)中,生成ETag(用于缓存)的答案是在Response对象上设置一些值.如下所示:
@GET
@Path("/person/{id}")
public Response getPerson(@PathParam("id") String name, @Context Request request){
Person person = _dao.getPerson(name);
if (person == null) {
return Response.noContent().build();
}
EntityTag eTag = new EntityTag(person.getUUID() + "-" + person.getVersion());
CacheControl cc = new CacheControl();
cc.setMaxAge(600);
ResponseBuilder builder = request.evaluatePreconditions(person.getUpdated(), eTag);
if (builder == null) {
builder = Response.ok(person);
}
return builder.cacheControl(cc).lastModified(person.getUpdated()).build();
}
Run Code Online (Sandbox Code Playgroud)
问题是对我们不起作用,因为我们对SOAP和REST服务使用相同的方法,通过使用@WebMethod(SOAP),@ GET(以及我们可能需要公开服务的任何其他方法)来注释方法.以前的服务对我们来说是这样的(不包括标题的创建):
@WebMethod
@GET
@Path("/person/{id}")
public Person getPerson(@WebParam(name="id") @PathParam("id") String name){
return _dao.getPerson(name);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法 - 通过一些额外的配置 - 设置这些标题?这是我第一次发现使用Response对象实际上只比自动转换有一些好处...
我们正在使用Apache CXF.
我有一个jax-rs服务,它接收路径中的一组参数pathparameters.这些参数可能是包含不适合url的值的字符串,因此它们在客户端使用java.net.UrlEncoder进行urlencoded,如下所示:
String param = URLEncoder.encode(o.toString(), "UTF-8");
Run Code Online (Sandbox Code Playgroud)
这用于构建URL supplier/group/param1/param2/param3.如果其中一个因urlencoding而被更改,例如,如果它只是一个空格,则在服务上接收的字符串是一个+符号.
@GET
@Path("{supplierId}/{groupCode}/{groupId}")
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
public SupplierGroup getSupplierGroup(@PathParam("supplierId") BigDecimal supplierId,
@PathParam("groupCode") String groupCode,
@PathParam("groupId") BigDecimal groupId) {
//now groupCode is "+", not " "
}
Run Code Online (Sandbox Code Playgroud)
我希望jaxrs能够自动解码编码路径参数.
编辑:测试更多我发现,当发送使用%20空间时,它能够解码参数.
我有一个异常映射器如下
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class NotFoundMapper implements ExceptionMapper<NotFoundException> {
private final Logger log = LoggerFactory.getLogger(getClass());
private final MapperResponseBuilder responseBuilder = new MapperResponseBuilder();
@Override
public Response toResponse(NotFoundException ex) {
log.warn("NotFoundException : " + ex.getMessage(), ex);
return responseBuilder.buildErrorResponse(ex.getMessage(), Status.BAD_REQUEST);
}
}
Run Code Online (Sandbox Code Playgroud)
所以NotFoundException是一个RuntimeException.我想有3个异常映射器,它们映射
有没有办法优先考虑那些?
我想通过外部枚举定义从JAX-RS端点生成一个招摇,但生成的swagger直接包含枚举到模型的定义中.这意味着不会生成枚举文档,而是在客户端复制相同的枚举.
我使用swagger-jaxrs依赖项来扫描我的端点并生成swagger json文件.此GitHub 存储库可用于重现该问题.我还在swagger-core存储库上创建了一个GitHub 问题.
@Api("hello")
@Path("/helloSwagger")
public class HelloSwagger {
@ApiOperation(value = "Get all unique customers", notes = "Get all customers matching the given search string.", responseContainer = "Set", response = User.class)
@GET
@Path("/getUniqueUsers")
@Produces(MediaType.APPLICATION_JSON)
public Set<User> getUniqueUsers(
@ApiParam(value = "The search string is used to find customer by their name. Not case sensitive.") @QueryParam("search") String searchString,
@ApiParam(value = "Limits the size of the result set", defaultValue = "50") @QueryParam("limit") int limit
) { …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×7
jersey ×3
rest ×3
jackson ×2
caching ×1
content-type ×1
cxf ×1
download ×1
encoding ×1
enums ×1
etag ×1
java-ee ×1
parameters ×1
pretty-print ×1
swagger ×1
web-services ×1