我正在构建一个Rest Client使用jersey-client 2.19:
public ReleaseEntity createRelease(ReleaseEntity newRelease, int workspaceId) {
Releases wrapper = new Releases();
wrapper.setData(Arrays.asList(newRelease));
WebTarget target = client.target(urlPrefix)
.path(AgmUrls.getReleasesUrl(workspaceId));
wrapper = target
.request()
.accept(MediaType.APPLICATION_JSON)
.post(Entity.entity(wrapper, MediaType.APPLICATION_JSON))
.readEntity(Releases.class);
return wrapper.getData().get(0);
}
Run Code Online (Sandbox Code Playgroud)
客户端在初始化 constructor
this.client = ClientBuilder.newClient();
Run Code Online (Sandbox Code Playgroud)
问题是,如果响应不好,post调用不会抛出异常,也不会抛出explicit异常runtime。
我应该手动执行此操作,还是我遗漏了什么?
从 Rest 服务抛出的异常是否可能以 JSON 形式返回?我有一个 JAX-RS Rest 服务,我想在其中实现这一目标。当我现在抛出它时,它会映射到 HTML 响应,这不是我想要的。据我了解,ExceptionMapper 也会将其映射到 HTML?是否有任何其他替代方案或库允许以 JSON 格式返回异常?
考虑以下 Spring MVC 注释:
@RequestMapping(value="content",
method=RequestMethod.GET,
produces = "application/json; charset=UTF-8")
Run Code Online (Sandbox Code Playgroud)
JAX-RS/Jersey 中的等效项是:
@GET
@Path("content")
@Produces(MediaType.APPLICATION_JSON)
Run Code Online (Sandbox Code Playgroud)
我正在寻找以下 JAX-RS/Jersey 注释的等效 Spring MVC 注释:
@Context@FormParm@BeanParam我们开始将 Jersey/JAX-RS 用于前端代码使用的内部 REST 端点。必须返回结果的端点始终发送 JSON 对象。
出于调试目的,我们使用 firefox restclient扩展。直到最近,我只会输入 URL 并点击发送,然后会返回显示为 JSON 的内容。
但是当我今天早上这样做时,FF 扩展返回并告诉我必须将响应类型更改为二进制 (BLOB)。这样做会导致显示编码字符串而不是 JSON。
我可以通过设置请求标头(Accept:to be application/json)来解决这个问题。
做更多的研究,我遇到了这个问题。我的结论是:可能我们应该添加@Produces("application/json")所有这些端点。
问题:真的那么简单,还是有很好的技术理由不这样做?
我正在使用带有Jersey 2.1 的Spring Boot
和 Ionic 来开发一个应用程序,我已经尝试了我找到的每一个帖子,但没有一个帖子为我解决了这个问题,我得到的错误,包括那些关于@PATCH自己创建界面注释的内容。
因此,问题是在执行PATCH请求时在浏览器上进行测试时,我在客户端收到此错误:
CORS 策略已阻止从源 ' http://localhost:8100 '访问 XMLHttpRequest :预检响应中的 Access-Control-Allow-Methods 不允许方法 PATCH。
我用于响应的过滤器如下,如果我有PATCH一个允许的方法并且它在 Postman 上完美运行,我不明白为什么我会得到这个:
筛选:
@Provider
public final class ResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Accept-Patch", "*");
headers.putSingle("Access-Control-Allow-Origin", "*");
headers.putSingle("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE, PATCH");
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Max-Age", "3600");
headers.putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
}
}
Run Code Online (Sandbox Code Playgroud)
使用的方法PATCH:
import javax.ws.rs.PATCH; …Run Code Online (Sandbox Code Playgroud) 到目前为止,我已经使用 Jersey,并且正在使用 JSON-B 进行第一次实现。
我正在使用 Payara,所以我与 Jersey 和 Yasson 一起工作。我遇到了问题,因为序列化日期始终包含“[UTC]”后缀。
我已设法在 DTO 中的日期属性上使用注释。但我想全局配置(在 JAX-RS 应用程序配置中?),而不是在每个日期属性上重复自己。那可能吗?到目前为止我还没有发现任何东西...
附带问题:我认为可以去掉这个“[UTC]”后缀,因为它会破坏所有尝试解析日期的客户端。任何想法?
我正在从 Java Jax-Rs API 迁移到 ASP.NET。我有传递到 API 的自定义标头参数。
我找不到在 ASP.NET 中执行相同操作的方法。
这是我到目前为止所拥有的:
[HttpPost]
public String login()
{
return "works";
}
Run Code Online (Sandbox Code Playgroud)
我搜索了我找到的每个教程,但找不到任何提及这一点的内容。
我正在使用Resteasy服务器端模拟框架来测试我的服务.我不想测试业务逻辑,但我想测试服务生成的数据.
使用这种方法,我可以创建一个简单的测试.但是,在我的RestEasy服务中,我有一些依赖,我想嘲笑.
请参阅以下我要测试的示例服务.必须嘲笑协作者,以便测试服务.
@Path("v1")
Class ExampleService {
@inject
private Collaborator collaborator;
@GET
@Path("/")
@Produces({ "application/xml", "application/json" })
public Response getDummy() throws WSAccessException, JsonParseException, JsonMappingException, IOException {
...
Result result = collaborator.getResult();
..
return Response.ok("helloworld").build();
}
}
Run Code Online (Sandbox Code Playgroud)
junit测试如下
@Test
public void testfetchHistory() throws URISyntaxException {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
POJOResourceFactory noDefaults = new POJOResourceFactory(ExampleService.class);
dispatcher.getRegistry().addResourceFactory(noDefaults);
MockHttpRequest request = MockHttpRequest.get("v1/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
Assert.assertEquals(..);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在测试中嘲笑合作者?
我正在使用Apache-cxf来实现Restful Web服务.如果发生异常,我正在使用ExceptionMapper来构建响应对象.如果发生任何异常,我会收到以下错误.
"没有为响应类MyException找到消息正文编写器."
我可以找到一些建议自定义Writer实现MessageBodyWriter的帖子,但我不太清楚为什么我需要一个自定义编写器,如果为构建响应传递的实体对象(ErrorInfo)是jaxb对象.这可能是一个非常愚蠢的问题,但只是想了解.
@Provider
public class MyExceptionMapper implements
ExceptionMapper<MyException> {
@Override
public Response toResponse(MyException ex) {
Response.Status statusCode = exceptionMap.get(ex.getClass());
ErrorInfo errorInfo=new ErrorInfo();
errorInfo.setErrorCode(ex.getErrorCode());
errorInfo.setErrorMessage(ex.getMessage());
return Response.status(statusCode).entity(ex).build();
}
}
@XmlRootElement(name = "errorInfo")
@XmlType(propOrder = { "errorCode", "errorMessage"})
public class ErrorInfo {
private String errorCode;
private String errorMessage;
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
} …Run Code Online (Sandbox Code Playgroud) 我正在使用JAX-RS使用平针织实现.我正在尝试使用Tomcat 6使用BASIC身份验证来验证我的服务.
这是代码:
@Path("/authenticate")
@RolesAllowed({"Admin","Guest"})
public class BasicAuthenticationSecurity {
@GET
@Path("/wbiPing")
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed("Admin")
public Response wbiPing(){
System.out.println("Pinged!!!");
return Response.ok("Pinged!!!").build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用注释我的方法时@RolesAllows,我收到编译错误:
@RolesAllows cannot be resolved to a type
请让我知道如何解决这个问题?这需要任何特定的罐子/ API?
编辑:
web.xml中
<servlet>
<servlet-name>jersey-serlvet</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.security;
com.exception
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>BasicDemo</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>Admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<!-- The realm name is typically displayed by the browser in the login …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×5
jersey ×4
exception ×2
rest ×2
web-services ×2
c# ×1
cors ×1
http-headers ×1
json ×1
jsonb-api ×1
junit ×1
patch ×1
payara ×1
resteasy ×1
spring-boot ×1
spring-mvc ×1
unit-testing ×1
yasson ×1