How do I know what implementation of jaxrs I am using? - java

I have a Java 1.7 starts application. It does have some RESTful api's.
I would like to add #JsonIgnore to a filed so that it is not returned in the api.
E.g.
Member.java
import com.fasterxml.jackson.annotation.JsonIgnore;
private java.lang.String username;
#JsonIgnore
private java.lang.String password;
Does not ignore the password.
"member": {
"password": "**************",
"username": "richard"
}
I think the reason why #JsonIgnore does not work, is because I use com.fasterxml.jackson.annotation.JsonIgnore. Should I use a different annotation from a different library? i.e. Is my implementation of jaxrs maybe not com.fasterxml.jackson? How do I tell?
The IntelliJ classpath has:
(I have tried net.minidev.json.annotate.JsonIgnore with no success)
More info:
pom.xml
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
and
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
Is this the problem? two versions! I am not sure where the 2.0.5 version comes from, as it is not defined in the pom.

You don't need a #JsonIgnore in this case. You can simply omit the variable that you don't want deserialized(the password in this case) and jackson will just return you the username.

Related

java springboot validate request parameters springboot #Valid annotation is invalid

Adding #NotBlank annotation to member variables in DTO does not take effect. What is the reason? Please help me
first check this dependency is present in the pom.xml file. If it doesn't present then add.
With dependency present in your project now use can use #NotBlack and #Valid annotation.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
For more information about Validation in Spring Boot check this link
Apart from including spring-boot-starter-validation in dependency, you must include #Valid annotation on #RequestBody in the controller for #NotBlank annotation to apply on member variables in DTO .

Validation beans are not working in Java 11 and spring boot 2.5.3

Hello after spending hours reading stackoverflow answers and trying to find the answer, I'm still unable to find out why validators such as NotNull and NotBlank are not working in java. I can still send post requests with a blank name. The project is on Java 11 and Spring boot starter v2.5.3. Thank you very much in advance if you are able to figure it out, I feel I must buy you an ice cream :')
In my model:
import javax.validation.constraints.NotBlank;
#NotBlank
private final String name;
In my controller:
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
#PostMapping(path="add")
public void addUser(#Valid #NotNull #RequestBody User user) {
//Request body takes the data taken and converts it into a User
userService.addUser(user);
}
#PutMapping(path="user/{id}")
public void updateUser(#PathVariable("id") UUID id, #Valid #NotNull #RequestBody User userToUpdate) {
userService.updateUser(id, userToUpdate);
}
In my dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
I had the same issue, but I could not find out the reason. the only solution was to use older JDK. I change to JDK1.8, and it is working fine.
I had the same error. I excluded 'hibernate-validator' dependency which was coming from some other dependency (you can see this by the dependency tree) and Added these 2 dependencies and it worked fine.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency>

Annotations from javax.validation.constraints not working (ignored)

I'm trying to use javax constraints to pre-validate a request content before running the logic.
I have tried any possible solution out there but still can't put javax annotations to work in a Spring boot.
import javax.validation.constraints.Min
import javax.validation.constraints.Pattern
data class LoginRequest (
#Credential //Custom constraint that works just fine
val credential: String,
#Min(value= 5)
val password: String,
#Pattern(regexp = Constants.Regex.DEVICE_ID_REGEX, message = "Invalid device ID")
val device: String
): Serializable
This is a part of pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
.
.
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.17.Final</version>
</dependency>
And this is the controller
import javax.validation.Valid
#PostMapping("/login")
fun userLoginEndpoint(#Valid #RequestBody loginRequest: LoginRequest): ResponseEntity<User> {
return authService.loginUser(loginRequest)
?.let{ ResponseEntity(it, HttpStatus.ACCEPTED)}
?: ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()
}
Am I doing something wrong?
From #JBNizet comment
Replace #Min(value= 5) (and the other validation annotations too, of course) by #field:Min(value= 5).

org.springframework.web.HttpMediaTypeNotAcceptableException Spring MVC 4.2.5

Im trying to get json response from my Spring Controller. Im getting below exception
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
I have jackson related jars in pom.xml as below
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<servletapi.version>2.5</servletapi.version>
<jackson.version>2.6.3</jackson.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
Below is my controller
#ResponseBody
#RequestMapping(value = "/getNodes.htm",produces="application/json")
public List<NodeDTO> getNodes() {
...
return nodes;
}
Im getting 406 error in the browser and above exception in server log. My browser is properly sending Accept header has application/json. But still Im facing this issue. I have gone through many forums none of them resolving my issue. Please help me to resolve it
Your #RequestMapping annotation is incorrect, in particular the use of the extension .htm.
It should be
#RequestMapping(value = "/getNodes.json",produces="application/json")
or perhaps
#RequestMapping(value = "/getNodes",produces="application/json")
Spring MVC uses the extension of the URL to identify the type of content to return. In your case, you're specifying an extension of .htm, which Spring interprets as HTML. That however conflicts with the produces property of your annotation, which specifies JSON.
It's not clear to me exactly how Spring handles conflicting content types such as this, but if you replace the .htm extension with .json or drop the extension altogether, then you don't need to worry about it.

Spring 4.2.3 with Hibernate validator 5.2.2.Final does not work

I am trying to integration Spring 4.2.3 with hibernate validator v.5.2.2.Final to validate input JSON data expose with REST Controller.
I don't see any compile time or running time exception but at the same time it does not validate input data.
Pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- jsr303 validation dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.2.2.Final</version>
</dependency>
Pojo java class:
public class RequestVO {
#NotEmpty
private String testValidation;
#NotNull
private String testNull;
#NotBlank
private String testBlank;
.... getters and setters .....
}
#Controller
#RequestMapping("/login")
public class LoginController {
#RequestMapping(method=RequestMethod.POST, value="/user")
public #ResponseBody ResponseVO getLoginResponse(#Valid #RequestBody RequestVO request, Errors error) {
ResponseVO response = new ResponseVO ();
if (error.hasErrors()) {
System.err.println("Success");
}
return response;
}
}
In your dependency list there's a comment saying ...jsr303 validation dependencies.... Note the bean validation has an updated specification (JSR-349), and this can very well be your issue.
You should align your dependencies in the following manner
hibernate-validator-5.x.x
validation-api-1.1.x
which implement JSR-349
OR
hibernate-validator-4.x.x
validation-api-1.0.x.
which implements JSR-303
If you mix the dependencies, the validation will simply not kick-in. As a solution suggestion, add
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
and make sure that you remove any dependency to validation-api-1.0 jar
I think is interest to take a look at this link
and if you to remove the validation just remove #Valid from method and you're ok.

Categories

Resources