hognkun
hognkun

Reputation: 347

how to change object to string(java spring boot)

when i run the code i got the object

[[Ljava.lang.Object;@8f17f7c, [Ljava.lang.Object;@6c0a4f24, 
 [Ljava.lang.Object;@be4886c, [Ljava.lang.Object;@1760591d, 
 [Ljava.lang.Object;@14e9ce12, [Ljava.lang.Object;@2aa4c0c4, 
 [Ljava.lang.Object;@5ac9a14]

so.. i want to get the String result which in the below plz teach me the way

[Dataset_info(Ds_id=1111, ds_code=a, ds_name=e, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null), Dataset_info(Ds_id=11111, ds_code=z, ds_name=eww, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null)]

@Data
@Entity
@Table(name = "category")
@DynamicInsert
@DynamicUpdate
@NoArgsConstructor
@AllArgsConstructor
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "category_id", columnDefinition = "INT(11)")
    private Integer Category_id;
    @Column(name = "name", columnDefinition = "VARCHAR(20)")
    private String name;
    @Column(name = "parent", columnDefinition = "int(11)")
    private Integer parent;

}

this is my Category Code

@RestController
@RequestMapping(value = "/Category")
@Slf4j
public class CategoryController {
    @Autowired CategoryRepository categoryRepository;

    @RequestMapping(value = "/all", method =
        RequestMethod.GET)
    @ResponseBody
    public String getCategoryList() {
        List < Object[] > all =
            this.categoryRepository.findByCategory();
        return all.toString();
        //log.info(query);
        //return "Test";
    }
}

this is my CategoryController code

import java.util.List;


@Repository
public interface CategoryRepository extends
JpaRepository < Category, Integer > {
    public static final String FIND_PROJECTS = "SELECT t1.name 
    AS lev1,
    t2.name as lev2,
    t3.name as lev3,
    t4.name as lev4
    FROM category AS t1 LEFT JOIN category AS t2 ON t2.parent =
    t1.category_id LEFT JOIN category AS t3 ON t3.parent =
    t2.category_id LEFT JOIN category AS t4 ON t4.parent =
    t3.category_id WHERE t1.name = 'ROOT'
    ";

    @Query(value = FIND_PROJECTS, nativeQuery = true)
    public List < Object[] > findByCategory();

}

this is my CategoryRepository Code

private void mysql2() {

    this.categoryRepository.findByCategory();
}

this is my application Code for running

so plz teach me i crave to know the way thank you

Upvotes: 0

Views: 1456

Answers (2)

sovannarith cheav
sovannarith cheav

Reputation: 793

You can use Projection to contain these values :

public interface CategoryProjection {
   public String getLev1();
   public String getLev2();
   public String getLev3();
   public String getLev4();
}

Then use the interface Projection with Repository :

//...
@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<CategoryProjection> findByCategory();

How to access values in Projections

Because It's a interface, only have getter method.

  • Using Loop (foreach loop, fori loop, ...)

ex :

List<CategoryProjection> list = categoryRepository.findByCategory();

list.forEach(c -> {
      System.out.println(c.getLev1() + " - " + c.getLev2());
});
// loop i
for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).getLev1() + " - " + list.get(i).getLev2());         
}
  • Using index

ex : to get Object Category in index 0

String lev1 = list.get(0).getLev1();

Upvotes: 1

Holinc
Holinc

Reputation: 743

UPD: In your case, I think you can change the method return type to List, HttpMessageConverter would convert the result as JSON String to client. Hope it help.

@ResponseBody
public List getCategoryList() {
    List<Object[]> all = this.categoryRepository.findByCategory();
    return all;
}

Override toString() method in your POJO object. For example:

public class Category {
    private Integer Category_id;
    private String name;
    private Integer parent;
    //omitted getter/setter

    @Override
    public String toString() {
        return "Category{" +
                "Category_id=" + Category_id +
                ", name='" + name + '\'' +
                ", parent=" + parent +
                '}';
    }
}

Upvotes: 0

Related Questions