Last Updated
Viewed 19 Times
  

I have a java resource class, where I am fetching details of a group, based on the group I am getting some details, those are like group info, I have an object and I am putting these group info inside that object.

But when i am fetching the info of the group and converting it into string, and putting this as

    // I am passing the group as agruement
    GroupModel groupJson = new GroupModel();
    groupJson.setGroupId(group.getGroupId());
    groupJson.setGroupName(channelGroup.getGroupName());
    obj.put("groupJson", groupJson.toJson()); // this toJSON funtion is giving me stringify of the object

but when I am sending this response to my Node server it is adding the extra forward slashes. So I have to parse the response twice,

  • Parsing 1 the entire object parsing,

  • parsing 2 for the object property of groupJson

// Inside javascript i have to do this
data = JSON.parse('{"groupId":"abcd1234","groupJson":"{\\"groupId\\":\\"abcd1234\\",\\"groupName\\":\\"group for songs\\"}"}')

JSON.parse(data.groupJson)
{groupId: "abcd1234", groupName: "group for songs"}

anyone know how to solve this?

Am using Java 1.7, RestEasy, and codehaus version of Jackson (for JSON parsing)...

Have a standard Java POJO DTO which populates a directory path (as a String) based on a Windows 7 filesystem:

import org.codehaus.jackson.annotate.JsonProperty;

public class BackupDirDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("id")
    private Long id;

    @JsonProperty("backup-dir")
    private String backupDir;

    // Getters & Setters
}

This is the layer which my business logic occurs... It gets the value from an entity from a database and sets the property in the DTO:

public class DirectoryRepository {

    public BackupDirDTO getDirectoryById(Long id) throws Exception {
        DirectoryEntity entity = directoryRespository.getEntityById(id);
        BackupDirDTO dto = new BackupDirDTO();
        log.debug("Entity.getBackupDir: " + enity.getBackupDirectory());
        dto.setBackupDir(entity.getBackupDirectory());
        log.debug("dto.getBackupDirectory: " + dto.getBackupDir());
        return dto;
    }
}

From my log.debug() calls it looks like this (which is the correct String that I want):

Entity.getBackupDir: "\\my-network-folder\backupDir"
dto.getBackupDirectory: \\my-network-folder\backupDir"

Here's the RESTful Web Service call:

public class DirectoryResource {

    private DirectoryRepository repository;

    @GET
    @Path("/directories/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public BackupDirDTO getDirectoryById(@PathParam("id") Long id) {
        BackupDirDTO dto = new BackupDirDTO();
        try {
            dto = repository.getDirectoryById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }
}

The actual JSON payload generated from the RESTful Web Service call looks like this:

{"id": 1, "backup-dir": "\\\\my-network-folder\\backupDir"}

Why is it adding an extra slashes?

My pom.xml

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson-provider</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.spec.javax.ws.rs</groupId>
    <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxb-provider</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jdk-http</artifactId>
    <version>2.3.10.Final</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.10.Final</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.jboss.spec.javax.servlet</groupId>
    <artifactId>jboss-servlet-api_3.0_spec</artifactId>
    <scope>provided</scope>
</dependency>

Tried using the Apache Commons library like this (inside the DirectoryRepository.getDirectoryById(Long id) method):

dto.setBackupDir(FilenameUtils.separatorsToUnix(entity.getBackupDirectory()));
dto.setBackupDir(FilenameUtils.separatorsToSystem(entity.getBackDirectory()));
dto.setBackupDir(FilenameUtils.separatorsToWindows(entity.getBackDirectory()));

The only one that doesn't include the extra slashes is (but I need the one for the Windows convention):

FilenameUtils.separatorsToUnix();

Even tried using @JSonRawValue inside my actual web service class:

@JsonRawValue
@JsonProperty("backup-dir")
private String backupDir;

This actually broke a lot of things...

Are the newly added slashes a result in the Web Services layer or Repository layer?

How can I make it more Windows specific?

What I want my JSON payload to look like is this:

{"id": 1, "backup-dir": "\\my-network-folder\backupDir"}

Thank you for taking the time to read this...

Similar Question 2 : Java adding extra passed variables

First of all, I should probably mention that I'm pretty new to Java.

I'm using PircBot and JDBC to make an irc bot that returns some MySQL values. The problem I'm having is that I want to make prepared statements to protect from SQL injection. However, because I can't add extra variables into my method, I can't seem to get it to work.

This is my code as of now: http://pastebin.com/K8zCKt9f

Basically, I want to use prepared SQL statements, but I can't pass Connection conn into the onMessage method without it messing up (onMessage does not trigger on IRC). This means that I'd have to create a new connection every time there's an if statement, as opposed to how it works right now with the MySQL method.

I'm sure there's an easier/cleaner way to do this, but I can't seem to figure it out. Any help is appreciated, thanks!

I want to parse a windows path in java. this is part of the json:

..."sslkey":"c:\usr\ssl\key.jks"

and this is the code:

JsonObject jsonargs = gson.fromJson(args[0], JsonObject.class);
SSLKEY = jsonargs.get("sslkey").getAsString();

I tried with \ also in the value, but the error that I have is:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 133 path $.sslkey

Could you please tell me how I can fix this?

Thank you !

Similar Question 5 (2 solutions) : JSON object adding extra back slash

Similar Question 9 (1 solutions) : Java zip util adding extra folders

cc