Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Spring Boot Rest client

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the following exception

The client code is

The controller is

In the debugger the controller is working fine.
Initially I had the same problem returning an ArrayList, so I changed the code to return an array. Currently I want to return a String; I will represent every object by a JSON string and I will write the JSON strings in the return String separated by new lines '\n'.  I still don't see a way to convert Java objects to JSON strings and how to construct Java objects from JSON Strings.
 
Saloon Keeper
Posts: 12409
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be using @RestController for web API controllers. Spring will automatically convert the type returned from your controller methods to the appropriate JSON response body, given that you've set the media types on your controller correctly.

Note that you're essentially performing an SQL injection attack on your own service. This is a REALLY bad idea. Attackers will use it to break your application or harm your users.

If you're looking to perform CRUD applications on a repository through a REST controller, you might want to use Spring Data REST instead.
 
Sheriff
Posts: 22015
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Note that you're essentially performing an SQL injection attack on your own service. This is a REALLY bad idea. Attackers will use it to break your application or harm your users.


I warned Alejandro about that in https://coderanch.com/t/733980/frameworks/org-springframework-web-client-HttpServerErrorException#3413974.
 
Alejandro Barrero
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using @RestController didn't make any difference.
 
Rob Spoor
Sheriff
Posts: 22015
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This 500 comes from your server. That means that the server is throwing an exception, that Spring Boot turns into a 500. What do the server logs say?
 
Bartender
Posts: 1955
13
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic