我很早就在REST实现中,并且最近了解到我们可以将JAX-RS注释放在我们的Java服务接口而不是类实现上.
对我来说,这似乎可能导致一个干净的类文件,但也可能导致开发人员不得不经常混淆文件.
每种方法的优缺点是什么?
我们在Apache CXF和Jackson之上运行了一整套JAX-RS REST服务.我们使用JAXB注释来处理将POJO编组为JSON,效果很好.
但是,我们有一两个地方要返回原始JSON字符串(我们从Redis缓存中获取).
杰克逊总是将字符串用双引号括起来,并将其中的所有双引号都删掉,例如
@GET @Produces("application/json")
public Response getData() {
String json = ...get from Redis...
return Response.ok(json,"application/json").build()
}
Run Code Online (Sandbox Code Playgroud)
给我们
"{\"test\":1}"
Run Code Online (Sandbox Code Playgroud)
代替
{"test":1}
Run Code Online (Sandbox Code Playgroud)
我尝试过多种方法,将RawSerializer(String.class)添加到Object映射器中,没有任何效果.唯一有效的是,如果我将媒体类型设置为普通字符串,绕过杰克逊,但是不好,因为我返回了错误的内容类型
即
return Response.ok(json,"text/plain").build()
Run Code Online (Sandbox Code Playgroud)
工作,但很差(错误的内容类型,这搞砸了.Net WCF应用程序打电话给我们)
Apache CXF(2.7.0)是否可以自动发现类路径中的JAX-RS资源?也就是说,带有注释的类@Path.
我在Spring应用程序中使用CXF,我必须使用以下XML手动声明资源,即使Spring已成功发现资源<context:component-scan ...>.
<jaxrs:server id="myService" address="/myService">
<jaxrs:serviceBeans>
<ref bean="myResource1" />
<ref bean="myResource2" />
<ref bean="myResource3" />
</jaxrs:serviceBeans>
</jaxrs:server>
Run Code Online (Sandbox Code Playgroud)
我想避免它(因为我可以使用其他JAX-RS实现,例如resteasy)因为在我的情况下它更难维护,并且它迫使我在Spring XML配置文件中声明我的bean依赖项.
我正在阅读"使用JAX-RS 2.0的RESTful Java"一书.我完全混淆了异步JAX-RS,所以我把所有问题都集中在一起.这本书写了这样的异步服务器:
@Path("/customers")
public class CustomerResource {
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public void getCustomer(@Suspended final AsyncResponse asyncResponse,
@Context final Request request,
@PathParam(value = "id") final int id) {
new Thread() {
@Override
public void run() {
asyncResponse.resume(Response.ok(new Customer(id)).build());
}
}.start();
}
}
Run Code Online (Sandbox Code Playgroud)
Netbeans创建这样的异步服务器:
@Path("/customers")
public class CustomerResource {
private final ExecutorService executorService = java.util.concurrent.Executors.newCachedThreadPool();
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public void getCustomer(@Suspended final AsyncResponse asyncResponse,
@Context final Request request,
@PathParam(value = "id") final int id) {
executorService.submit(new Runnable() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我使用jeresy ClientRespone.getEntity进行反序列化时遇到问题
我试图遵循一些教程和问题,包括:http : //jersey.576304.n2.nabble.com/How-can-I-parse-a-java-util-List-lt-gt-Is-它支持泽西客户端-tdd2300852.html https://jersey.java.net/nonav/documentation/1.5/json.html http://www.programcreek.com/java-api-examples/的index.php?API = com.sun.jersey.api.client.GenericType
我仍然一遍又一遍地得到同样的例外
我的目标是:而不是:
response.getEntity(String.class); --> {"name":"Ben","type":"The man","id":0}
Run Code Online (Sandbox Code Playgroud)
然后解析它(例如使用Jackson),我想让实体进入我的POJO对象.
到目前为止,这是我的尝试:
服务器端:
@POST
@Path("/account") // route to a specific method.re
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response saveDataIntoHash(Account account) {
Account createdAccount = new Account(account.getName(), account.getType());
accountHash.put(createdAccount.getID(), createdAccount);
return Response.status(201).entity(new AccountResponse(createdAccount.getID())).build();
}
Run Code Online (Sandbox Code Playgroud)
服务器端帐户类:
private String name;
private String type;
private int ID;
private static int classID = 0;
public Account(String name, String type) {
this.name = name;
this.type = type;
this.ID = classID++;
}
public Account() …Run Code Online (Sandbox Code Playgroud) 我收到了对此Web服务的GET响应
@GET
@Path("/nnnnnn")
public Response pfpfpfpf(@BeanParam NNNNNN n)
Run Code Online (Sandbox Code Playgroud)
该班NNNNN有:
@QueryParam("parameter")
private String parameter;
Run Code Online (Sandbox Code Playgroud)
为此,parameter有一个得到和设置.
我发送了一个带有查询参数的get请求,它正在自动绑定到我的选项NNNNN,一切都很棒.
但是,现在我在查询网址中发送日语字符串.我在发送之前用UTF-8对参数进行编码,我必须使用UTF-8解码它们.
但我的问题是我应该在哪里调用URLDecoder?我试图在该参数的getter中调用它,但它不起作用,我一直有类似的东西,C3%98%C2%B4%C3%98%C2 而不是日语字符
如何<param-value>为<param-name>servlet的<init-param>标记下的给定指定多个.以下是我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
参数jersey.config.server.provider.packages定义了Jersey将在哪个包中查找Web服务类.
我想在这里指定多个包名,因为我的服务类在不同的包中.像下面这样的东西,但它们都不起作用:
1)多个 <param-value>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
<param-value>com.vogella.jersey.second</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
2)多个 <init-param>
<servlet>
...
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.second</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
任务:不是HTTP 500 Internal Server Error在我的堆栈跟踪中接收常规,而是在客户端使用相同的可怕堆栈跟踪,我希望看到我的自定义消息与另一个状态代码(403例如),对于开发人员来说,它会更清楚,发生了什么.并向用户添加一些关于异常的消息.
以下是我的应用程序中的几个更改的类:
服务器部分:
AppException.class - 我的所有服务器响应异常(在回馈客户端之前)我想转换为此异常.有点标准实体类
public class AppException extends WebApplicationException {
Integer status;
/** application specific error code */
int code;
/** link documenting the exception */
String link;
/** detailed error description for developers */
String developerMessage;
public AppException(int status, int code, String message, String developerMessage, String link) {
super(message);
this.status = status;
this.code = code;
this.developerMessage = developerMessage;
this.link = link;
}
public int getStatus() {
return status; …Run Code Online (Sandbox Code Playgroud) 应StreamingOutput的OutputStream由实现类被关闭?
java-doc没有给出任何建议.我想它只是代表底层ServletOutputStream,这意味着它不应该被关闭,但我的猜测可能是错误的:)此外,javadoc引用了一个MessageBodyWriter接口,它明确表示输出流不能被关闭.
https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/StreamingOutput.html
将使用CXF3.x构建的JAX-RS服务从weblogic 12.1.3升级到12.2.1后,我面临以下奇怪的问题
<03-ago-2017, 3:22:38,789 PM CEST> <Error> <HTTP> <BEA-101216> <Servlet: "JAX-RS/Jersey#1" failed to preload on startup in Web application: "sife".
A MultiException has 1 exceptions. They are:
1. java.lang.LinkageError: ClassCastException: attempting to castzip:C:/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/AdminServer/tmp/_WL_user/sife/845176/war/WEB-INF/lib/javax.ws.rs-api-2.0.1.jar!/javax/ws/rs/ext/RuntimeDelegate.class to jar:file:/C:/Oracle/Middleware/Oracle_Home/oracle_common/modules/javax.ws.rs.javax.ws.rs-api.jar!/javax/ws/rs/ext/RuntimeDelegate.class
at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:1007)
at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:962)
at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1054)
at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:1046)
at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:696)
Truncated. see log file for complete stacktrace
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的是CXF,而不是Jersey,但似乎Weblogic正在尝试使用它(12.1.3不会发生这种情况).我按照以下建议在配置文件中禁用了它:https://jerometambo.github.io/blog/2016/12/13/How-to-use-CXF-3x-implementation-of-JAX-RS-20-REST-与-WebLogic的12c.html
使用默认配置,Weblogic希望使用其内部实现(JAX-RS 2.0和序列化)来部署REST Web服务(错误500):
META-INF/weblogic的-application.xml中
<weblogic-application xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-application"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-application
http://xmlns.oracle.com/weblogic/weblogic-application/1.8/weblogic-application.xsd">
<prefer-application-packages>
<package-name>javax.ws.rs.*</package-name>
<package-name>com.fasterxml.jackson.*</package-name>
<package-name>weblogic.jaxrs.api.client.*</package-name>
<package-name>weblogic.jaxrs.internal.api.*</package-name>
<package-name>weblogic.jaxrs.dispatch.*</package-name>
<package-name>weblogic.jaxrs.monitoring.util.*</package-name>
</prefer-application-packages>
</weblogic-application>
Run Code Online (Sandbox Code Playgroud)
WEB-INF/weblogic.xml中
<weblogic-web-app
xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd …Run Code Online (Sandbox Code Playgroud) jax-rs ×10
java ×9
jersey ×5
cxf ×4
rest ×3
json ×2
spring ×2
web-services ×2
annotations ×1
asynchronous ×1
decoding ×1
jackson ×1
java-ee ×1
servlets ×1
urldecode ×1
web.xml ×1
weblogic12c ×1