Tag: jax rs

处理泽西岛中不匹配的内容类型

我一直收到表格的错误: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded 所以我试图防范这个错误,如下所示: @Path(“auth”) @Produces(MediaType.APPLICATION_JSON) public class Auth { @POST @Path(“login”) @Consumes(MediaType.WILDCARD) public Response create() { return Response.status(Status.UNSUPPORTED_MEDIA_TYPE).build(); } @POST @Path(“login”) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response create(@Valid @NotNull @FormParam(“username”) final String username, @Valid @NotNull @FormParam(“password”) final String password) { 但是,这并不能解决问题。 生成的FINE日志消息非常垃圾。 我有两个问题: […]

RESTful Webservice无法正确处理Request Method

我正在从多个RESTful Web服务方法中检索值。 在这种情况下,由于请求方法的问题,两种方法相互干扰。 @GET @Path(“/person/{name}”) @Produces(“application/xml”) public Person getPerson(@PathParam(“name”) String name) { System.out.println(“@GET /person/” + name); return people.byName(name); } @POST @Path(“/person”) @Consumes(“application/xml”) public void createPerson(Person person) { System.out.println(“@POST /person”); System.out.println(person.getId() + “: ” + person.getName()); people.add(person); } 当我尝试使用以下代码调用createPerson()方法时,我的Glassfish服务器将导致“@GET / person / 我试图创建一个人的名字 ”。 这意味着调用了@GET方法,即使我没有发送{name}参数(正如您在代码中看到的那样)。 URL url = new URL(“http://localhost:8080/RESTfulService/service/person”); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(“POST”); […]

cxf客户端中的SNI配置(3.1.2)

我想在使用CXF客户端(3.1.2)进行rest呼叫时设置自定义SNI主机名(SNI配置)。 我正在使用java 8.我能够使用HTTPClient做同样的事情(参见下面的强文本代码剪切以供参考),但我无法弄清楚如何使用CXF客户端做同样的事情。 //对于HTTP客户端 private SSLConnectionSocketFactory createSSLConnectionSocketFactory(String sniHostanme, SSLContext sslContext){ // Fix for host name verifier, need to implement———————- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { “TLSv1.2” }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()) { @Override protected void prepareSocket(SSLSocket socket) throws IOException { try { // System.out.println(“************ setting socket HOST property *************”); // If SNI is required if […]

Resteasy和fileupload:没有内容处理错误

我有一个可以上传文件的rest资源。 @Path(“/rest/input”) public class InputResourceBean { @POST @Path(“{directory: .*}”) @Consumes(“multipart/form-data”) public void post(final String directory, final MultipartFormDataInput input) { } 我已经使用SOAPUI,curl和HTML表单来测试这段代码,并且对于所有这些代码我都得到了同样的错误:在部分内部找不到Content-Disposition标头 12:55:19,739 WARN [org.jboss.resteasy.core.SynchronousDispatcher] Failed executing POST /rest/input/myDir: org.jboss.resteasy.spi.ReaderException: java.lang.RuntimeException: Could find no Content-Disposition header within part at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:123) [:6.0.0.Final] at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:93) [:6.0.0.Final] at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:146) [:6.0.0.Final] at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:114) [:6.0.0.Final] at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [:6.0.0.Final] at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:252) [:6.0.0.Final] […]

如何使用Jersey的内部路由机制来提取类/方法引用?

我正在运行Jersey 1.8应用程序。 Jersey正在作为Servlet运行。 我需要编写一个给出普通请求/响应的servletfilter ,能够确定哪个REST资源/方法将响应请求并从注释中提取值。 例如,假设我有以下资源: @Path(“/foo”) @MyAnnotation(“hello”) public class FooResource { @GET @Path(“/bar”) @MyOtherAnnotation(“world”) public Response bar(){ … } } 当请求GET /foo/bar进来时,我需要我的servletfilter能够在Jersey自己的servlet处理请求之前从MyAnnotation和MyOtherAnnotation提取值”hello”和”world” 。 此filter逻辑应该能够适用于所有请求和注册的所有资源。 有没有办法访问Jersey的内部路由机制来获取Jersey将发送请求的类/方法引用? 我也对其他建议持开放态度,但理想情况下,通过自己阅读@Path注释来试图破解我自己的路由机制就没有了。

JAX-RS自定义标头未添加到重定向响应中

我试图将一些自定义标头参数添加到HTTP 303(重定向)响应。 但是,新标题似乎从响应中被剥离。 此代码用于接收请求并返回HTTP 303响应: @POST @Path(“/authorize”) @Produces(“application/x-www-form-urlencoded”) public Response getOAuthGrant(@HeaderParam(OAuth2.AUTHORIZATION) @DefaultValue(“”) String authorization, @HeaderParam(OAuth2.CLIENT_ID) @DefaultValue(“”) String clientId, @HeaderParam(OAuth2.CLIENT_SECRET) @DefaultValue(“”) String clientSecret, @HeaderParam(OAuth2.GRANT_TYPE) @DefaultValue(“”) String grantType) throws InternalServerException, UnauthorizedException { OAuth2.validateGrantRequest(clientId, clientSecret, authorization, grantType); ApiTokenV2 apiTokenV2 = new ApiTokenV2(); try { apiTokenV2 = TokenManager.getApiToken(clientId); if (apiTokenV2 != null) { apiTokenV2.generateAccessGrant(clientId); } else { logger.error(“Error in […]

使用SSL和代理设置的Rest Client获取连接timeOut

我使用Rest客户端忽略ssl它工作正常但它将来不会用于我尝试使用客户端证书的生产。 我有ca证书和客户证书我用它创建了一个客户端,但我收到了错误 Exception in thread “main” com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection timed out: connect at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155) at com.sun.jersey.api.client.Client.handle(Client.java:652) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) at org.app.last.JerseyClient.get_JSON(JerseyClient.java:67) at org.app.last.JerseyClient.main(JerseyClient.java:266) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:529) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:564) at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141) at sun.net.NetworkClient.doConnect(NetworkClient.java:163) at sun.net.www.http.HttpClient.openServer(HttpClient.java:395) at […]

Restful webservices – NoClassDefFoundError:org / glassfish / jersey / ExtendedConfig

所以我在这个线程中创建了webservice,最后我设法解决了这个问题。 现在我正在尝试使用这个Web服务。 我在Netbeans上创建了一个新的Web项目,我正在使用Apache Tomcat。 这是使用Web服务的代码。 我已经完成了一些教程来生成这段代码。 package com.client; import java.net.URI; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import org.glassfish.jersey.client.ClientConfig; public class HelloClient { public void consumeRest(){ ClientConfig config = new ClientConfig(); Client client = ClientBuilder.newClient(config); WebTarget target = client.target(getBaseURI()); System.out.println(“response”); System.out.println(target.path(“hello”).path(“world”).request() .accept(MediaType.APPLICATION_JSON).get(Response.class) .toString()); } private URI getBaseURI() { return UriBuilder.fromUri(“http://localhost:8084/restful_example”).build(); […]

如何使用JAX-RS转发请求?

我想将REST请求转发给另一台服务器。 我使用JAX-RS与Jersey和Tomcat。 我尝试设置See Other响应并添加Location标头,但它不是真正的前进。 如果我使用: request.getRequestDispatcher(url).forward(request, response); 我明白了: java.lang.StackOverflowError :如果url是相对路径 java.lang.IllegalArgumentException :路径http://website.com不以/字符开头(我认为转发仅在同一个servlet上下文中合法)。 我该如何转发请求?

JAX-RS:如何将我的对象列表作为JSON返回?

我看了jackson的文档,它让我困惑:(我的实体看起来像: @Entity @Table(name = “variable”) public class Variable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column(unique = true, nullable = false) private String name; @Column @Enumerated(EnumType.STRING) private VariableType type; @Column(nullable = false) private String units; @Temporal(TemporalType.TIMESTAMP) @Column(name = “created_on”, nullable = false) private Date createdOn; @Temporal(TemporalType.TIMESTAMP) @Column(name = “retired_on”, nullable = true) private Date […]