我有一个包含这样的元素的html表单
<input type="text" value="Val1" name="Name1"/>
<input type="text" value="Val2" name="Name2"/>
<input type="hidden" value="Val3" name="Name3"/>
Run Code Online (Sandbox Code Playgroud)
在服务器端,我使用Jersey实现来捕获表单名称和值.有没有办法在这样的单个地图中捕捉上述所有内容
Name1 ==> Val1 Name2 ==> Val2 Name3 ==> Val3
我理解使用@FormParam,我可以捕获变量中的表单值.但我需要捕获表单元素名称以及值.
任何帮助表示赞赏.
目前我正在尝试创建一个只返回列表的Web服务;
@Path("/random")
@Singleton
public class Random
{
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public MyResult<String> test()
{
MyResult<String> test = new MyResult<String>();
test.add("Awesome");
return test;
}
}
Run Code Online (Sandbox Code Playgroud)
我的MyResult类看起来像这样:
@XmlRootElement
public class MyResult<T> implements Iterable<T>
{
private ArrayList<T> _items;
private int _total;
public MyResult()
{
_items = new ArrayList<T>();
}
public ArrayList<T> getItems()
{
return _items;
}
public void setItems(ArrayList<T> items)
{
_items = items;
}
public int getTotal()
{
return _total;
}
public void setTotal(int total)
{
_total = total;
}
public void …Run Code Online (Sandbox Code Playgroud) 我在jax-rs应用程序中有一些资源我想在jax-rs请求进入由javax.ws.rs.Path注释的资源之前验证它.那么,我怎样才能为我的资源创建处理程序或过滤器.我搜索了很多网站.他们的建议是使用代理或servlet过滤器.不使用代理或servlet过滤器我可以创建处理程序/过滤器?
就像在JAX-WS中一样,SOAPHandler可用于soap请求,同样有任何处理程序用于验证jax-rs请求.
这里验证jax-rs请求意味着预检,后检查和异常处理..(我使用的是泽西罐)
我正在使用RestEasy 3.0.2,它是最早的JAX-RS 2实现之一,并在Tomcat 7中运行我的应用程序.我还通过WELD在我的应用程序中使用注入,WELD通过其CDI适配器与RestEasy集成.到目前为止一切正常.
现在,我编写了一个ContainerRequestFilter的实现,以在传入请求到达资源之前对其进行身份验证.JAX-RS标准表示可以为每个资源以及使用@Provider注释注释的每个其他JAX-RS组件进行注入.
以下是我的过滤器实现的简化版本:
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
@Inject
AuthenticationProvider authenticationProvider;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
authenticationProvider.authenticate(requestContext);
}
}
Run Code Online (Sandbox Code Playgroud)
注意:AuthenticationProvider是@RequestScoped.
通常,此解决方案有效.正在注入组件并按预期处理请求.
但我仍然怀疑过滤器的生活范围.如果它是应用程序作用域,那么这显然会导致"有趣"的并发问题,这些问题在确定性测试中无法找到.
我已经查看了各种文档,指南和示例,但我发现没有使用过滤器注入或者说过滤器范围.
我使用下面的AngularJS客户端代码来执行带有JSON格式有效负载的HTTP post请求到jersey rest服务
patientMgmtModule.controller('NewPatientCtrl',
function NewPatientCtrl($scope, $http)
{
$scope.addPatient = function (){
var patientJSON = angular.toJson($scope.Patient);
console.log("Patient (JSON) is ============> " + patientJSON);
$http({
method: 'POST',
data: $scope.Patient,
url:'/ManagePatient/AddPatient',
headers: {'Content-Type':'application/x-www-form-urlencoded;application/json;'}
});
};
Run Code Online (Sandbox Code Playgroud)
});
我对Jersey有以下maven依赖项:
<dependency>
<groupId>org.glassfish.jersey.archetypes</groupId>
<artifactId>jersey-quickstart-webapp</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在服务器端,我有
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.hms.app.ui.beans.Patient;
@Path("/ManagePatient")
public class PatientController {
@POST
@Path("/AddPatient")
@Consumes({MediaType.APPLICATION_JSON})
public String addPatient(Patient patient) {
System.out.println("Sarat's servlet called" );
//patient.toConsole();
System.out.println("Done Calling …Run Code Online (Sandbox Code Playgroud) 我有一个POJO,我需要序列化为JSON.POJO有很多属性,我想将其中的一些属性序列化为JSON表示.我正在使用杰克逊序列化.由于我想忽略很多属性,因此将每个属性注释为@JsonIgnore看起来非常难看
有没有办法告诉jackson或将objectMapper配置为仅在注释为@JsonProperty时包含属性,并忽略其余而不指定@JsonIgnore.
我已将我的Web应用程序升级到JAX-RS 2.0.
Web应用程序似乎在Apache Tomcat上运行良好.但是,它不会部署在Weblogic 12c(甚至10.3.6)上.
我不确定weblogic是否有适当的支持,我相信它需要一些配置和类加载器过滤来覆盖默认的JAX-RS 1.1实现?
知道如何实现这一点并让我的Web应用程序在WLS 12c上运行吗?
这是班级: -
package com.bablo.rest;
import javax.websocket.server.PathParam;
import javax.ws.rs.Path;
@Path("/")
public class Library {
@Produces("text/plain")
@Path("/books/{name}")
public String getBook(@PathParam("name") String name){
System.out.println(name);
return "My Name is Anthony Goncalves";
}
}
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误
子资源定位器public java.lang.String com.bablo.rest.Library.geBook(java.lang.String)不能具有实体参数.尝试将参数移动到相应的资源方法.
和
在索引0处的参数处缺少方法public java.lang.String com.bablo.rest.Library.getBook(java.lang.String)的依赖项
我正在通过浏览器调用这个web服务
http://localhost:8080/JAXRS-HelloWorld/rest/books/bablo
Run Code Online (Sandbox Code Playgroud)
我也在做卷毛:
curl -X GET http://localhost:8080/JAXRS-HelloWorld/rest/books/bablo
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用JAX-RS注释将一些EJB公开为REST Web服务.当我将war包含EJB Jar的文件部署WEB-INF/lib到Wildfly 8中时,我可以在Web管理面板EJB Jar中看到已部署,但是我无法访问REST端点并获得404.
这是以下内容web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/separated/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
这是一个示例会话bean我正在尝试作为Web服务并放入jar文件中:
@Stateless(name = "TestSessionEJB")
@LocalBean
public class TestSessionBean {
@PersistenceContext(unitName = "TestPU")
private EntityManager em;
public AuthenticationSessionBean() {
}
@GET
@Path("ep")
public String testEP() {
return "Hello from testEP()";
}
}
Run Code Online (Sandbox Code Playgroud)
我无法testEP通过/<war_file_name>/separated/ep.添加了ejb-jar.xml描述符WEB-INF/,仍然没有成功.我用直接在war文件中编译和部署的类创建了另一个服务WEB-INF/classes:
@ApplicationPath("/integrated")
public class TestRestApp extends Application {
} …Run Code Online (Sandbox Code Playgroud) 我使用以下代码来使用休息服务
Client client = ClientBuilder.newClient();
WebTarget target = client.target("wrong url");
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.post(Entity.entity(param, MediaType.APPLICATION_JSON), Response.class);
Run Code Online (Sandbox Code Playgroud)
正如所料,我收到了错误.和状态代码400 Bad Request.但我没有收到错误消息.当我运行时response.getStatusInfo()我得到了错误的请求,但我的服务器发送了额外的信
当我使用postman我在Body窗口中获取错误信息时调用它.
那么如何从响应对象中获取此错误正文信息?或任何其他方式???