如何{"status" : "successful"}在JAX-RS中自动映射一个简单的JSON对象映射到我的Java Enum?
public enum Status {
SUCESSFUL ("successful"),
ERROR ("error");
private String status;
private Status(String status) {
this.status = status;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要进一步的细节请随时问:)
我有一个像这样的宁静的Web服务方法:
@GET
@Path("/generateInfo")
@Produces(MediaType.APPLICATION_JSON)
public String generateInfo(
@QueryParam("a") String a,
@QueryParam("b") String b,
@QueryParam("date") Date date) {
// ...business code...
return "hello world";
}
Run Code Online (Sandbox Code Playgroud)
如何从WebBrowser中调用该方法?问题是Date当我尝试给我404找不到或500内部服务器错误时的参数.
我有一个通用的JAX-RS资源类,我已经定义了一个泛型findAll方法
public abstract class GenericDataResource<T extends GenericModel> {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findAll() {
Query query = em.createNamedQuery(modelClass.getSimpleName()+".findAll");
List<T> list = query.getResultList();
return Response.ok(new GenericEntity<List<T>>(list) {}).build();
}
}
Run Code Online (Sandbox Code Playgroud)
和用户类:
public class User extends GenericModel {
...
}
Run Code Online (Sandbox Code Playgroud)
这里是示例子类定义:
@Path("users")
public class UserResource extends GenericDataResource<User> {
public UserResource() {
super(User.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
com.sun.jersey.api.MessageException: A message body writer for Java class
java.util.Vector, and Java type java.util.List<T>,
and MIME media type application/json was not found exception.
Run Code Online (Sandbox Code Playgroud)
如果我用定义的类替换T,例如User:
GenericEntity<List<User>>(list) …
我正在尝试发布到需要使用以下代码设置Content-Length标头的Web服务:
// EDIT: added apache connector code
ClientConfig clientConfig = new ClientConfig();
ApacheConnector apache = new ApacheConnector(clientConfig);
// setup client to log requests and responses and their entities
client.register(new LoggingFilter(Logger.getLogger("com.example.app"), true));
Part part = new Part("123");
WebTarget target = client.target("https://api.thing.com/v1.0/thing/{thingId}");
Response jsonResponse = target.resolveTemplate("thingId", "abcdefg")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "anauthcodehere")
.post(Entity.json(part));
Run Code Online (Sandbox Code Playgroud)
从发布说明https://java.net/jira/browse/JERSEY-1617和Jersey 2.0文档https://jersey.java.net/documentation/latest/message-body-workers.html,它暗示Content-长度自动设定.但是,我从服务器返回411响应代码,表明请求中不存在Content-Length.
有谁知道获取Content-Length标头集的最佳方法?
我已通过设置记录器验证了请求中未生成Content-Length标头.
谢谢.
我想将Jackson用作JAX-RS 2.0 Web服务的JSON提供程序.对于JAX-RS,我在GlassFish 4中使用Jersey 2.0.使用JAX-RS 1.x我可以添加
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
在我的web.xml.我怎么能用Jersey 2.0的Jax-RS 2.0做到这一点?我实现了这样的应用程序类
public class MyRESTExampleApplication extends ResourceConfig {
public MyRESTExampleApplication() {
packages("com.carano.fleet4all.restExample");
register(JacksonFeature.class);
}
}
Run Code Online (Sandbox Code Playgroud)
并将这些行添加到我的web.xml.
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.example.restExample.MyRESTExampleApplication</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
但是我通过请求org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException得到一个例外 :找不到媒体类型= application/json,type = class的MessageBodyWriter ...
我pom.xml看起来像这样
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud) 我创建了一个在本地实现REST服务的应用程序:
Eclipse Indigo Jersey 2.4 Tomcat 7.0.47
使用Eclipse在本地运行时,服务工作正常,但在部署我的WAR文件时,尝试对其中一个服务URL执行GET时会出现以下异常:
HTTP Status 500 - Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
type Exception report
message Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet.init() for servlet com.app.rest.MyResourceConfig threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
java.lang.Thread.run(Thread.java:662)
root cause
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:270)
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:218)
org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:448)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:300)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) …Run Code Online (Sandbox Code Playgroud) 我使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的歌班:
public class Song {
private String songname;
private String artist;
public String getSongname () { return this.songname; }
public String getArtist () { return this.artist; }
public void setSongname (String songname) { …Run Code Online (Sandbox Code Playgroud) 我需要查看我的请求主体与客户端JAX-RS请求,以验证序列化是否正常,并重用请求测试客户端,如Postman.
我知道可以使用例如使用Jersey激活日志记录resource.addFilter(new com.sun.jersey.api.client.filter.LoggingFilter());.但是,我不直接使用Jersey或RESTEasy实现,而是通过JAX-RS API抽象:
final WebTarget target =
ClientBuilder.newBuilder().build().target("http://localhost:8080");
Run Code Online (Sandbox Code Playgroud)
如何在此处启用日志记录?
但是,有时close()来自代码片段的方法不会被JAX-RS实现调用(org.jboss.resteasy:resteasy-client-3.0.11.FINAL在本例中).
@FormDataParam和之间有什么区别@FormParam?
我@FormDataParam在一个方法中使用多个,但它抛出了媒体不支持的类型错误.但是当我使用时 @FormParam,我得到了价值观.
所以,我需要知道他们俩之间的区别是什么?
我无法通过以下链接找到答案
以下是我的pom.xml依赖项
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.41</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jvnet/mimepull -->
<dependency>
<groupId>org.jvnet</groupId>
<artifactId>mimepull</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<type>jar</type> …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×7
jersey ×5
rest ×4
jersey-2.0 ×2
json ×2
generics ×1
glassfish-4 ×1
jackson ×1
java-ee ×1
logging ×1
mapping ×1
moxy ×1
tomcat7 ×1
web-services ×1