如何在 jax-rs 检查 body
How to check body at jax-rs
我正在做 API 休息后端,稍后与 angular 相辅相成,我一直在考虑如果使用 Jersey 或 Spring Web,我选择了 Jersey。
我的实际 Profesor.class 有 3 个参数;姓名、年龄和主题。
但是当我用 Postman 发送下一个 JSON 时:
{
"name":"bob",
"age":"21",
"asd":"qwe"
}
它没有给出错误,正在工作并在 MySQL 添加主题为空的教授,我希望球衣的响应告诉我类似 "wrong model" 或类似的东西,并且不要击中数据库。这是我得到的答案:
{
"name": "bob",
"age": 21,
"subject": null
}
有什么想法吗?
这就是我的控制器实际工作的方式(我正在使用 Spring Boot)
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CreaterController {
@Autowired
CreateService createService;
@POST
@Path("/profesor")
public Response createProfesor(Profesor profesor) {
return createService.crearProfesor(profesor);
}
....
这是我的 CreateServiceImpl
@Service
public class CreateServiceImpl implements CreateService {
@Autowired
private ProfesorDao profesorDao;
@Autowired
private AlumnoDao alumnoDao;
@Autowired
private MateriaDao materiaDao;
public Response crearProfesor(Profesor profesor) {
System.out.println("Creando nuevo profesor...");
return Response.ok(profesorDao.save(profesor)).build();
}
...
默认情况下,spring 和球衣中的请求 bean(教授)都没有进行验证。
如果您需要请求 bean 验证,您应该在您的 bean class(教授)中添加额外的验证登录。添加 bean 验证因 jersey 和 Spring 而异,您可以参考以下链接以获取有关如何
的更多详细信息
- 泽西岛 Reference Link
- Spring Reference Link
我正在做 API 休息后端,稍后与 angular 相辅相成,我一直在考虑如果使用 Jersey 或 Spring Web,我选择了 Jersey。
我的实际 Profesor.class 有 3 个参数;姓名、年龄和主题。 但是当我用 Postman 发送下一个 JSON 时:
{
"name":"bob",
"age":"21",
"asd":"qwe"
}
它没有给出错误,正在工作并在 MySQL 添加主题为空的教授,我希望球衣的响应告诉我类似 "wrong model" 或类似的东西,并且不要击中数据库。这是我得到的答案:
{
"name": "bob",
"age": 21,
"subject": null
}
有什么想法吗? 这就是我的控制器实际工作的方式(我正在使用 Spring Boot)
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CreaterController {
@Autowired
CreateService createService;
@POST
@Path("/profesor")
public Response createProfesor(Profesor profesor) {
return createService.crearProfesor(profesor);
}
....
这是我的 CreateServiceImpl
@Service
public class CreateServiceImpl implements CreateService {
@Autowired
private ProfesorDao profesorDao;
@Autowired
private AlumnoDao alumnoDao;
@Autowired
private MateriaDao materiaDao;
public Response crearProfesor(Profesor profesor) {
System.out.println("Creando nuevo profesor...");
return Response.ok(profesorDao.save(profesor)).build();
}
...
默认情况下,spring 和球衣中的请求 bean(教授)都没有进行验证。
如果您需要请求 bean 验证,您应该在您的 bean class(教授)中添加额外的验证登录。添加 bean 验证因 jersey 和 Spring 而异,您可以参考以下链接以获取有关如何
的更多详细信息- 泽西岛 Reference Link
- Spring Reference Link