GSON not imported into Maven project

I get the following error: I added GSon to my dependency -

Can someone point out what I'm doing wrong?

enter image description here

Edit: The indicated dependency is -

<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>1.7.1</version> </dependency> 

I use the following code in my sevlet -

  JSONService json = new JSONService(); String json_output = json.makeLoginJSON(user); 

makeLoginJSON ---

  public String makeLoginJSON(LoginDetails user) { String FinalJson = null; Gson gson = new Gson(); FinalJson = gson.toJson(user); return FinalJson; } 
+7
source share
3 answers

Try:

 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> 

I edited my comment and added an area. The default scope is compile, which means that the dependency is not present at run time. To do this, you use the provided area. Learn more about Apache maven dependency scopes An introduction to Maven dependencies .

Hope this solves your problem.

PS: if you create your own repository, you should also look here .

+16
source

Just an explanation in Raoul’s answer - it’s good that it works for you, but is provided for dependencies that are expected to be available from the JRE / JDK, i.e. servlet classes. Compilation is by default and should work in cases of other people, since gson is not accessible from the JRE, instead it must be loaded by maven. From http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope :

compilation This is the default area that is used if none are specified. Compilation dependencies are available in all classes of the project. In addition, these dependencies apply to dependent projects.

provided This is very similar to compilation, but indicates that you expect the JDK or container to provide a dependency at runtime. For example, when creating a web application for Java Enterprise Edition, you must make a dependency on the Servlet API and its associated Java EE APIs for scope because the web container provides these classes. This scope is accessible only on the way to compilation and testing and is not transitive.

at run time This area indicates that the dependency is not required to compile, but is intended to be executed. It is in the runtime and tests class paths, but does not compile class paths.

test This area indicates that the dependency is not required for normal use of the application and is available only for the compilation and execution phases of the tests.

system This scope is the same as provided, except that you must provide a JAR that contains it explicitly. The artifact is always available and cannot be viewed in the repository.

import (available only in Maven 2.0.9 or later) This scope is used only depending on the type of pom in this section. It indicates that the specified POM should be replaced by the dependencies in this POM section. Since they are replaced, dependencies with the import region do not actually participate in limiting the transitivity of the dependency.

+3
source

Try:

 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.21</version> </dependency> 

Very fast, easy to use.

 VO obj = ...; String jsonString = JSON.toJSONString(obj); VO obj2 = JSON.parseObject(jsonString, VO.class); 
-1
source

Source: https://habr.com/ru/post/920075/


All Articles